blob: dea1466f0ba185953a12abfa77aceac041557961 [file] [log] [blame]
Eric Christophere243fd92011-04-03 22:34:07 +00001//===- Object.cpp - C bindings to the object file library--------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the C bindings to the file-format-independent object
11// library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Object/ObjectFile.h"
16#include "llvm-c/Object.h"
17
18using namespace llvm;
19using namespace object;
20
Owen Anderson3cb05672011-10-21 17:50:59 +000021// ObjectFile creation
Eric Christophere243fd92011-04-03 22:34:07 +000022LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
23 return wrap(ObjectFile::createObjectFile(unwrap(MemBuf)));
24}
25
26void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
27 delete unwrap(ObjectFile);
28}
29
Owen Anderson3cb05672011-10-21 17:50:59 +000030// ObjectFile Section iterators
Eric Christophere243fd92011-04-03 22:34:07 +000031LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000032 section_iterator SI = unwrap(ObjectFile)->begin_sections();
33 return wrap(new section_iterator(SI));
Eric Christophere243fd92011-04-03 22:34:07 +000034}
35
36void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
37 delete unwrap(SI);
38}
39
40LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
41 LLVMSectionIteratorRef SI) {
42 return (*unwrap(SI) == unwrap(ObjectFile)->end_sections()) ? 1 : 0;
43}
44
45void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +000046 error_code ec;
47 unwrap(SI)->increment(ec);
48 if (ec) report_fatal_error("LLVMMoveToNextSection failed: " + ec.message());
Eric Christophere243fd92011-04-03 22:34:07 +000049}
50
Owen Andersone2fa64e2011-10-21 18:21:22 +000051void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
52 LLVMSymbolIteratorRef Sym) {
53 if (error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
54 report_fatal_error(ec.message());
55}
56
Owen Anderson3cb05672011-10-21 17:50:59 +000057// ObjectFile Symbol iterators
58LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
59 symbol_iterator SI = unwrap(ObjectFile)->begin_symbols();
60 return wrap(new symbol_iterator(SI));
61}
62
63void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
64 delete unwrap(SI);
65}
66
67LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
68 LLVMSymbolIteratorRef SI) {
69 return (*unwrap(SI) == unwrap(ObjectFile)->end_symbols()) ? 1 : 0;
70}
71
72void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
73 error_code ec;
74 unwrap(SI)->increment(ec);
75 if (ec) report_fatal_error("LLVMMoveToNextSymbol failed: " + ec.message());
76}
77
78// SectionRef accessors
Eric Christophere243fd92011-04-03 22:34:07 +000079const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +000080 StringRef ret;
81 if (error_code ec = (*unwrap(SI))->getName(ret))
82 report_fatal_error(ec.message());
83 return ret.data();
Eric Christophere243fd92011-04-03 22:34:07 +000084}
85
86uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +000087 uint64_t ret;
88 if (error_code ec = (*unwrap(SI))->getSize(ret))
89 report_fatal_error(ec.message());
90 return ret;
Eric Christophere243fd92011-04-03 22:34:07 +000091}
92
93const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +000094 StringRef ret;
95 if (error_code ec = (*unwrap(SI))->getContents(ret))
96 report_fatal_error(ec.message());
97 return ret.data();
Eric Christophere243fd92011-04-03 22:34:07 +000098}
Owen Anderson3cb05672011-10-21 17:50:59 +000099
100uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
101 uint64_t ret;
102 if (error_code ec = (*unwrap(SI))->getAddress(ret))
103 report_fatal_error(ec.message());
104 return ret;
105}
106
Owen Andersonca7eb3e2011-10-21 20:35:58 +0000107LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
Owen Anderson3cb05672011-10-21 17:50:59 +0000108 LLVMSymbolIteratorRef Sym) {
109 bool ret;
110 if (error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
111 report_fatal_error(ec.message());
112 return ret;
113}
114
115// SymbolRef accessors
116const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
117 StringRef ret;
118 if (error_code ec = (*unwrap(SI))->getName(ret))
119 report_fatal_error(ec.message());
120 return ret.data();
121}
122
123uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
124 uint64_t ret;
125 if (error_code ec = (*unwrap(SI))->getAddress(ret))
126 report_fatal_error(ec.message());
127 return ret;
128}
129
130uint64_t LLVMGetSymbolOffset(LLVMSymbolIteratorRef SI) {
131 uint64_t ret;
132 if (error_code ec = (*unwrap(SI))->getOffset(ret))
133 report_fatal_error(ec.message());
134 return ret;
135}
136
137uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
138 uint64_t ret;
139 if (error_code ec = (*unwrap(SI))->getSize(ret))
140 report_fatal_error(ec.message());
141 return ret;
142}
143