1 //===- 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
18 using namespace llvm;
19 using namespace object;
20
21 // ObjectFile creation
LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf)22 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
23 return wrap(ObjectFile::createObjectFile(unwrap(MemBuf)));
24 }
25
LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile)26 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
27 delete unwrap(ObjectFile);
28 }
29
30 // ObjectFile Section iterators
LLVMGetSections(LLVMObjectFileRef ObjectFile)31 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
32 section_iterator SI = unwrap(ObjectFile)->begin_sections();
33 return wrap(new section_iterator(SI));
34 }
35
LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI)36 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
37 delete unwrap(SI);
38 }
39
LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,LLVMSectionIteratorRef SI)40 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
41 LLVMSectionIteratorRef SI) {
42 return (*unwrap(SI) == unwrap(ObjectFile)->end_sections()) ? 1 : 0;
43 }
44
LLVMMoveToNextSection(LLVMSectionIteratorRef SI)45 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
46 error_code ec;
47 unwrap(SI)->increment(ec);
48 if (ec) report_fatal_error("LLVMMoveToNextSection failed: " + ec.message());
49 }
50
LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,LLVMSymbolIteratorRef Sym)51 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
52 LLVMSymbolIteratorRef Sym) {
53 if (error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
54 report_fatal_error(ec.message());
55 }
56
57 // ObjectFile Symbol iterators
LLVMGetSymbols(LLVMObjectFileRef ObjectFile)58 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
59 symbol_iterator SI = unwrap(ObjectFile)->begin_symbols();
60 return wrap(new symbol_iterator(SI));
61 }
62
LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI)63 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
64 delete unwrap(SI);
65 }
66
LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,LLVMSymbolIteratorRef SI)67 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
68 LLVMSymbolIteratorRef SI) {
69 return (*unwrap(SI) == unwrap(ObjectFile)->end_symbols()) ? 1 : 0;
70 }
71
LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI)72 void 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
LLVMGetSectionName(LLVMSectionIteratorRef SI)79 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
80 StringRef ret;
81 if (error_code ec = (*unwrap(SI))->getName(ret))
82 report_fatal_error(ec.message());
83 return ret.data();
84 }
85
LLVMGetSectionSize(LLVMSectionIteratorRef SI)86 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
87 uint64_t ret;
88 if (error_code ec = (*unwrap(SI))->getSize(ret))
89 report_fatal_error(ec.message());
90 return ret;
91 }
92
LLVMGetSectionContents(LLVMSectionIteratorRef SI)93 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
94 StringRef ret;
95 if (error_code ec = (*unwrap(SI))->getContents(ret))
96 report_fatal_error(ec.message());
97 return ret.data();
98 }
99
LLVMGetSectionAddress(LLVMSectionIteratorRef SI)100 uint64_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
LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,LLVMSymbolIteratorRef Sym)107 LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
108 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 // Section Relocation iterators
LLVMGetRelocations(LLVMSectionIteratorRef Section)116 LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
117 relocation_iterator SI = (*unwrap(Section))->begin_relocations();
118 return wrap(new relocation_iterator(SI));
119 }
120
LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI)121 void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
122 delete unwrap(SI);
123 }
124
LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,LLVMRelocationIteratorRef SI)125 LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
126 LLVMRelocationIteratorRef SI) {
127 return (*unwrap(SI) == (*unwrap(Section))->end_relocations()) ? 1 : 0;
128 }
129
LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI)130 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
131 error_code ec;
132 unwrap(SI)->increment(ec);
133 if (ec) report_fatal_error("LLVMMoveToNextRelocation failed: " +
134 ec.message());
135 }
136
137
138 // SymbolRef accessors
LLVMGetSymbolName(LLVMSymbolIteratorRef SI)139 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
140 StringRef ret;
141 if (error_code ec = (*unwrap(SI))->getName(ret))
142 report_fatal_error(ec.message());
143 return ret.data();
144 }
145
LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI)146 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
147 uint64_t ret;
148 if (error_code ec = (*unwrap(SI))->getAddress(ret))
149 report_fatal_error(ec.message());
150 return ret;
151 }
152
LLVMGetSymbolFileOffset(LLVMSymbolIteratorRef SI)153 uint64_t LLVMGetSymbolFileOffset(LLVMSymbolIteratorRef SI) {
154 uint64_t ret;
155 if (error_code ec = (*unwrap(SI))->getFileOffset(ret))
156 report_fatal_error(ec.message());
157 return ret;
158 }
159
LLVMGetSymbolSize(LLVMSymbolIteratorRef SI)160 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
161 uint64_t ret;
162 if (error_code ec = (*unwrap(SI))->getSize(ret))
163 report_fatal_error(ec.message());
164 return ret;
165 }
166
167 // RelocationRef accessors
LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI)168 uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
169 uint64_t ret;
170 if (error_code ec = (*unwrap(RI))->getAddress(ret))
171 report_fatal_error(ec.message());
172 return ret;
173 }
174
LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI)175 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
176 uint64_t ret;
177 if (error_code ec = (*unwrap(RI))->getOffset(ret))
178 report_fatal_error(ec.message());
179 return ret;
180 }
181
LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI)182 LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
183 SymbolRef ret;
184 if (error_code ec = (*unwrap(RI))->getSymbol(ret))
185 report_fatal_error(ec.message());
186
187 return wrap(new symbol_iterator(ret));
188 }
189
LLVMGetRelocationType(LLVMRelocationIteratorRef RI)190 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
191 uint64_t ret;
192 if (error_code ec = (*unwrap(RI))->getType(ret))
193 report_fatal_error(ec.message());
194 return ret;
195 }
196
197 // NOTE: Caller takes ownership of returned string.
LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI)198 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
199 SmallVector<char, 0> ret;
200 if (error_code ec = (*unwrap(RI))->getTypeName(ret))
201 report_fatal_error(ec.message());
202
203 char *str = static_cast<char*>(malloc(ret.size()));
204 std::copy(ret.begin(), ret.end(), str);
205 return str;
206 }
207
208 // NOTE: Caller takes ownership of returned string.
LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI)209 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
210 SmallVector<char, 0> ret;
211 if (error_code ec = (*unwrap(RI))->getValueString(ret))
212 report_fatal_error(ec.message());
213
214 char *str = static_cast<char*>(malloc(ret.size()));
215 std::copy(ret.begin(), ret.end(), str);
216 return str;
217 }
218
219