blob: f5de2e1d5ce23b8cae7d7ff4d956ad589d61b22f [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
Eric Christophere243fd92011-04-03 22:34:07 +000015#include "llvm-c/Object.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000016#include "llvm/ADT/SmallVector.h"
Chandler Carruth974a4452014-01-07 11:48:04 +000017#include "llvm/Object/ObjectFile.h"
Eric Christophere243fd92011-04-03 22:34:07 +000018
19using namespace llvm;
20using namespace object;
21
Rafael Espindola548f2b62014-08-19 18:44:46 +000022inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) {
23 return reinterpret_cast<OwningBinary<ObjectFile> *>(OF);
Eric Christopher3e397312013-04-22 22:47:22 +000024}
25
Rafael Espindola548f2b62014-08-19 18:44:46 +000026inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) {
27 return reinterpret_cast<LLVMObjectFileRef>(
28 const_cast<OwningBinary<ObjectFile> *>(OF));
Eric Christopher3e397312013-04-22 22:47:22 +000029}
30
31inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
32 return reinterpret_cast<section_iterator*>(SI);
33}
34
35inline LLVMSectionIteratorRef
36wrap(const section_iterator *SI) {
37 return reinterpret_cast<LLVMSectionIteratorRef>
38 (const_cast<section_iterator*>(SI));
39}
40
41inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) {
42 return reinterpret_cast<symbol_iterator*>(SI);
43}
44
45inline LLVMSymbolIteratorRef
46wrap(const symbol_iterator *SI) {
47 return reinterpret_cast<LLVMSymbolIteratorRef>
48 (const_cast<symbol_iterator*>(SI));
49}
50
51inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) {
52 return reinterpret_cast<relocation_iterator*>(SI);
53}
54
55inline LLVMRelocationIteratorRef
56wrap(const relocation_iterator *SI) {
57 return reinterpret_cast<LLVMRelocationIteratorRef>
58 (const_cast<relocation_iterator*>(SI));
59}
60
Owen Anderson3cb05672011-10-21 17:50:59 +000061// ObjectFile creation
Eric Christophere243fd92011-04-03 22:34:07 +000062LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
Rafael Espindolab138cab2014-06-23 22:00:37 +000063 std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf));
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +000064 Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
Rafael Espindola548f2b62014-08-19 18:44:46 +000065 ObjectFile::createObjectFile(Buf->getMemBufferRef()));
66 std::unique_ptr<ObjectFile> Obj;
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +000067 if (!ObjOrErr) {
68 // TODO: Actually report errors helpfully.
69 consumeError(ObjOrErr.takeError());
Bjorn Steinbrinkea388502014-09-05 21:22:09 +000070 return nullptr;
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +000071 }
Bjorn Steinbrinkea388502014-09-05 21:22:09 +000072
73 auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf));
Rafael Espindola548f2b62014-08-19 18:44:46 +000074 return wrap(Ret);
Eric Christophere243fd92011-04-03 22:34:07 +000075}
76
77void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
78 delete unwrap(ObjectFile);
79}
80
Owen Anderson3cb05672011-10-21 17:50:59 +000081// ObjectFile Section iterators
Rafael Espindola548f2b62014-08-19 18:44:46 +000082LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) {
83 OwningBinary<ObjectFile> *OB = unwrap(OF);
84 section_iterator SI = OB->getBinary()->section_begin();
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000085 return wrap(new section_iterator(SI));
Eric Christophere243fd92011-04-03 22:34:07 +000086}
87
88void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
89 delete unwrap(SI);
90}
91
Rafael Espindola548f2b62014-08-19 18:44:46 +000092LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF,
93 LLVMSectionIteratorRef SI) {
94 OwningBinary<ObjectFile> *OB = unwrap(OF);
95 return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0;
Eric Christophere243fd92011-04-03 22:34:07 +000096}
97
98void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
Rafael Espindolaefdbec82014-01-30 02:49:50 +000099 ++(*unwrap(SI));
Eric Christophere243fd92011-04-03 22:34:07 +0000100}
101
Owen Andersone2fa64e2011-10-21 18:21:22 +0000102void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
103 LLVMSymbolIteratorRef Sym) {
Kevin Enderbya486dca2016-05-02 20:28:12 +0000104 Expected<section_iterator> SecOrErr = (*unwrap(Sym))->getSection();
105 if (!SecOrErr) {
106 std::string Buf;
107 raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000108 logAllUnhandledErrors(SecOrErr.takeError(), OS);
Kevin Enderbya486dca2016-05-02 20:28:12 +0000109 OS.flush();
110 report_fatal_error(Buf);
111 }
Rafael Espindolae84d8c12015-08-07 23:27:14 +0000112 *unwrap(Sect) = *SecOrErr;
Owen Andersone2fa64e2011-10-21 18:21:22 +0000113}
114
Owen Anderson3cb05672011-10-21 17:50:59 +0000115// ObjectFile Symbol iterators
Rafael Espindola548f2b62014-08-19 18:44:46 +0000116LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) {
117 OwningBinary<ObjectFile> *OB = unwrap(OF);
118 symbol_iterator SI = OB->getBinary()->symbol_begin();
Owen Anderson3cb05672011-10-21 17:50:59 +0000119 return wrap(new symbol_iterator(SI));
120}
121
122void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
123 delete unwrap(SI);
124}
125
Rafael Espindola548f2b62014-08-19 18:44:46 +0000126LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF,
127 LLVMSymbolIteratorRef SI) {
128 OwningBinary<ObjectFile> *OB = unwrap(OF);
129 return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0;
Owen Anderson3cb05672011-10-21 17:50:59 +0000130}
131
132void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
Rafael Espindolaefdbec82014-01-30 02:49:50 +0000133 ++(*unwrap(SI));
Owen Anderson3cb05672011-10-21 17:50:59 +0000134}
135
136// SectionRef accessors
Eric Christophere243fd92011-04-03 22:34:07 +0000137const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000138 StringRef ret;
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000139 if (std::error_code ec = (*unwrap(SI))->getName(ret))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000140 report_fatal_error(ec.message());
141 return ret.data();
Eric Christophere243fd92011-04-03 22:34:07 +0000142}
143
144uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
Rafael Espindola8175be52014-10-08 15:28:58 +0000145 return (*unwrap(SI))->getSize();
Eric Christophere243fd92011-04-03 22:34:07 +0000146}
147
148const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000149 StringRef ret;
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000150 if (std::error_code ec = (*unwrap(SI))->getContents(ret))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000151 report_fatal_error(ec.message());
152 return ret.data();
Eric Christophere243fd92011-04-03 22:34:07 +0000153}
Owen Anderson3cb05672011-10-21 17:50:59 +0000154
155uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
Rafael Espindola8175be52014-10-08 15:28:58 +0000156 return (*unwrap(SI))->getAddress();
Owen Anderson3cb05672011-10-21 17:50:59 +0000157}
158
Owen Andersonca7eb3e2011-10-21 20:35:58 +0000159LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
Owen Anderson3cb05672011-10-21 17:50:59 +0000160 LLVMSymbolIteratorRef Sym) {
Rafael Espindola8175be52014-10-08 15:28:58 +0000161 return (*unwrap(SI))->containsSymbol(**unwrap(Sym));
Owen Anderson3cb05672011-10-21 17:50:59 +0000162}
163
Owen Andersond8b0b912011-10-27 17:15:47 +0000164// Section Relocation iterators
165LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) {
Rafael Espindolaa40b3522014-02-10 20:24:04 +0000166 relocation_iterator SI = (*unwrap(Section))->relocation_begin();
Owen Andersond8b0b912011-10-27 17:15:47 +0000167 return wrap(new relocation_iterator(SI));
168}
169
170void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) {
171 delete unwrap(SI);
172}
173
174LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section,
175 LLVMRelocationIteratorRef SI) {
Rafael Espindolaa40b3522014-02-10 20:24:04 +0000176 return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0;
Owen Andersond8b0b912011-10-27 17:15:47 +0000177}
178
179void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) {
Rafael Espindolaefdbec82014-01-30 02:49:50 +0000180 ++(*unwrap(SI));
Owen Andersond8b0b912011-10-27 17:15:47 +0000181}
182
183
Owen Anderson3cb05672011-10-21 17:50:59 +0000184// SymbolRef accessors
185const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000186 Expected<StringRef> Ret = (*unwrap(SI))->getName();
187 if (!Ret) {
188 std::string Buf;
189 raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000190 logAllUnhandledErrors(Ret.takeError(), OS);
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000191 OS.flush();
192 report_fatal_error(Buf);
193 }
Rafael Espindola8a806412015-07-02 20:55:21 +0000194 return Ret->data();
Owen Anderson3cb05672011-10-21 17:50:59 +0000195}
196
197uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
Kevin Enderby317de7c2016-06-24 18:24:42 +0000198 Expected<uint64_t> Ret = (*unwrap(SI))->getAddress();
199 if (!Ret) {
200 std::string Buf;
201 raw_string_ostream OS(Buf);
Jonas Devlieghere686dfe32018-11-11 01:46:03 +0000202 logAllUnhandledErrors(Ret.takeError(), OS);
Kevin Enderby317de7c2016-06-24 18:24:42 +0000203 OS.flush();
204 report_fatal_error(Buf);
205 }
Rafael Espindola5954faa2015-07-03 18:19:00 +0000206 return *Ret;
Owen Anderson3cb05672011-10-21 17:50:59 +0000207}
208
Owen Anderson3cb05672011-10-21 17:50:59 +0000209uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
Rafael Espindola821b06f2015-06-24 10:20:30 +0000210 return (*unwrap(SI))->getCommonSize();
Owen Anderson3cb05672011-10-21 17:50:59 +0000211}
212
Owen Anderson3529c532011-10-27 17:32:36 +0000213// RelocationRef accessors
Danil Malyshevb0436a72011-11-29 17:40:10 +0000214uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
Rafael Espindolaff676292015-06-29 23:29:12 +0000215 return (*unwrap(RI))->getOffset();
Danil Malyshevb0436a72011-11-29 17:40:10 +0000216}
217
Owen Anderson3529c532011-10-27 17:32:36 +0000218LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) {
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000219 symbol_iterator ret = (*unwrap(RI))->getSymbol();
Owen Anderson3529c532011-10-27 17:32:36 +0000220 return wrap(new symbol_iterator(ret));
221}
222
223uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) {
Rafael Espindola7ede9642015-06-30 01:53:01 +0000224 return (*unwrap(RI))->getType();
Owen Anderson3529c532011-10-27 17:32:36 +0000225}
226
227// NOTE: Caller takes ownership of returned string.
228const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
229 SmallVector<char, 0> ret;
Rafael Espindolaf8a35ff2015-06-30 04:08:37 +0000230 (*unwrap(RI))->getTypeName(ret);
Serge Pavlov06c71d82018-02-20 05:41:26 +0000231 char *str = static_cast<char*>(safe_malloc(ret.size()));
Fangrui Song53a62242018-11-17 01:44:25 +0000232 llvm::copy(ret, str);
Owen Anderson3529c532011-10-27 17:32:36 +0000233 return str;
234}
235
236// NOTE: Caller takes ownership of returned string.
237const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) {
Rafael Espindola8c7a0fd2015-06-03 04:48:06 +0000238 return strdup("");
Owen Anderson3529c532011-10-27 17:32:36 +0000239}
240