Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 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 | |
Filip Pizlo | dec20e4 | 2013-05-01 20:59:00 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallVector.h" |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 16 | #include "llvm-c/Object.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 17 | #include "llvm/Object/ObjectFile.h" |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | using namespace object; |
| 21 | |
Eric Christopher | 04d4e93 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 22 | inline ObjectFile *unwrap(LLVMObjectFileRef OF) { |
| 23 | return reinterpret_cast<ObjectFile*>(OF); |
| 24 | } |
| 25 | |
| 26 | inline LLVMObjectFileRef wrap(const ObjectFile *OF) { |
| 27 | return reinterpret_cast<LLVMObjectFileRef>(const_cast<ObjectFile*>(OF)); |
| 28 | } |
| 29 | |
| 30 | inline section_iterator *unwrap(LLVMSectionIteratorRef SI) { |
| 31 | return reinterpret_cast<section_iterator*>(SI); |
| 32 | } |
| 33 | |
| 34 | inline LLVMSectionIteratorRef |
| 35 | wrap(const section_iterator *SI) { |
| 36 | return reinterpret_cast<LLVMSectionIteratorRef> |
| 37 | (const_cast<section_iterator*>(SI)); |
| 38 | } |
| 39 | |
| 40 | inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) { |
| 41 | return reinterpret_cast<symbol_iterator*>(SI); |
| 42 | } |
| 43 | |
| 44 | inline LLVMSymbolIteratorRef |
| 45 | wrap(const symbol_iterator *SI) { |
| 46 | return reinterpret_cast<LLVMSymbolIteratorRef> |
| 47 | (const_cast<symbol_iterator*>(SI)); |
| 48 | } |
| 49 | |
| 50 | inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) { |
| 51 | return reinterpret_cast<relocation_iterator*>(SI); |
| 52 | } |
| 53 | |
| 54 | inline LLVMRelocationIteratorRef |
| 55 | wrap(const relocation_iterator *SI) { |
| 56 | return reinterpret_cast<LLVMRelocationIteratorRef> |
| 57 | (const_cast<relocation_iterator*>(SI)); |
| 58 | } |
| 59 | |
Owen Anderson | f239db4 | 2011-10-21 17:50:59 +0000 | [diff] [blame] | 60 | // ObjectFile creation |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 61 | LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) { |
Rafael Espindola | 51cc360 | 2014-01-22 00:14:49 +0000 | [diff] [blame] | 62 | ErrorOr<ObjectFile*> ObjOrErr(ObjectFile::createObjectFile(unwrap(MemBuf))); |
| 63 | ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : 0; |
| 64 | return wrap(Obj); |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) { |
| 68 | delete unwrap(ObjectFile); |
| 69 | } |
| 70 | |
Owen Anderson | f239db4 | 2011-10-21 17:50:59 +0000 | [diff] [blame] | 71 | // ObjectFile Section iterators |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 72 | LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 73 | section_iterator SI = unwrap(ObjectFile)->begin_sections(); |
| 74 | return wrap(new section_iterator(SI)); |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) { |
| 78 | delete unwrap(SI); |
| 79 | } |
| 80 | |
| 81 | LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile, |
| 82 | LLVMSectionIteratorRef SI) { |
| 83 | return (*unwrap(SI) == unwrap(ObjectFile)->end_sections()) ? 1 : 0; |
| 84 | } |
| 85 | |
| 86 | void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) { |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame^] | 87 | ++(*unwrap(SI)); |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Owen Anderson | 07bfdbb | 2011-10-21 18:21:22 +0000 | [diff] [blame] | 90 | void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect, |
| 91 | LLVMSymbolIteratorRef Sym) { |
| 92 | if (error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect))) |
| 93 | report_fatal_error(ec.message()); |
| 94 | } |
| 95 | |
Owen Anderson | f239db4 | 2011-10-21 17:50:59 +0000 | [diff] [blame] | 96 | // ObjectFile Symbol iterators |
| 97 | LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) { |
| 98 | symbol_iterator SI = unwrap(ObjectFile)->begin_symbols(); |
| 99 | return wrap(new symbol_iterator(SI)); |
| 100 | } |
| 101 | |
| 102 | void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) { |
| 103 | delete unwrap(SI); |
| 104 | } |
| 105 | |
| 106 | LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile, |
| 107 | LLVMSymbolIteratorRef SI) { |
| 108 | return (*unwrap(SI) == unwrap(ObjectFile)->end_symbols()) ? 1 : 0; |
| 109 | } |
| 110 | |
| 111 | void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) { |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame^] | 112 | ++(*unwrap(SI)); |
Owen Anderson | f239db4 | 2011-10-21 17:50:59 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // SectionRef accessors |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 116 | const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) { |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 117 | StringRef ret; |
| 118 | if (error_code ec = (*unwrap(SI))->getName(ret)) |
| 119 | report_fatal_error(ec.message()); |
| 120 | return ret.data(); |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) { |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 124 | uint64_t ret; |
| 125 | if (error_code ec = (*unwrap(SI))->getSize(ret)) |
| 126 | report_fatal_error(ec.message()); |
| 127 | return ret; |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) { |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 131 | StringRef ret; |
| 132 | if (error_code ec = (*unwrap(SI))->getContents(ret)) |
| 133 | report_fatal_error(ec.message()); |
| 134 | return ret.data(); |
Eric Christopher | 9f08a3b | 2011-04-03 22:34:07 +0000 | [diff] [blame] | 135 | } |
Owen Anderson | f239db4 | 2011-10-21 17:50:59 +0000 | [diff] [blame] | 136 | |
| 137 | uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) { |
| 138 | uint64_t ret; |
| 139 | if (error_code ec = (*unwrap(SI))->getAddress(ret)) |
| 140 | report_fatal_error(ec.message()); |
| 141 | return ret; |
| 142 | } |
| 143 | |
Owen Anderson | 500ebeb | 2011-10-21 20:35:58 +0000 | [diff] [blame] | 144 | LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI, |
Owen Anderson | f239db4 | 2011-10-21 17:50:59 +0000 | [diff] [blame] | 145 | LLVMSymbolIteratorRef Sym) { |
| 146 | bool ret; |
| 147 | if (error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret)) |
| 148 | report_fatal_error(ec.message()); |
| 149 | return ret; |
| 150 | } |
| 151 | |
Owen Anderson | e245af6 | 2011-10-27 17:15:47 +0000 | [diff] [blame] | 152 | // Section Relocation iterators |
| 153 | LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) { |
| 154 | relocation_iterator SI = (*unwrap(Section))->begin_relocations(); |
| 155 | return wrap(new relocation_iterator(SI)); |
| 156 | } |
| 157 | |
| 158 | void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) { |
| 159 | delete unwrap(SI); |
| 160 | } |
| 161 | |
| 162 | LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section, |
| 163 | LLVMRelocationIteratorRef SI) { |
| 164 | return (*unwrap(SI) == (*unwrap(Section))->end_relocations()) ? 1 : 0; |
| 165 | } |
| 166 | |
| 167 | void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) { |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame^] | 168 | ++(*unwrap(SI)); |
Owen Anderson | e245af6 | 2011-10-27 17:15:47 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
Owen Anderson | f239db4 | 2011-10-21 17:50:59 +0000 | [diff] [blame] | 172 | // SymbolRef accessors |
| 173 | const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) { |
| 174 | StringRef ret; |
| 175 | if (error_code ec = (*unwrap(SI))->getName(ret)) |
| 176 | report_fatal_error(ec.message()); |
| 177 | return ret.data(); |
| 178 | } |
| 179 | |
| 180 | uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) { |
| 181 | uint64_t ret; |
| 182 | if (error_code ec = (*unwrap(SI))->getAddress(ret)) |
| 183 | report_fatal_error(ec.message()); |
| 184 | return ret; |
| 185 | } |
| 186 | |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 187 | uint64_t LLVMGetSymbolFileOffset(LLVMSymbolIteratorRef SI) { |
Owen Anderson | f239db4 | 2011-10-21 17:50:59 +0000 | [diff] [blame] | 188 | uint64_t ret; |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 189 | if (error_code ec = (*unwrap(SI))->getFileOffset(ret)) |
Owen Anderson | f239db4 | 2011-10-21 17:50:59 +0000 | [diff] [blame] | 190 | report_fatal_error(ec.message()); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) { |
| 195 | uint64_t ret; |
| 196 | if (error_code ec = (*unwrap(SI))->getSize(ret)) |
| 197 | report_fatal_error(ec.message()); |
| 198 | return ret; |
| 199 | } |
| 200 | |
Owen Anderson | f7a89d0 | 2011-10-27 17:32:36 +0000 | [diff] [blame] | 201 | // RelocationRef accessors |
| 202 | uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) { |
| 203 | uint64_t ret; |
| 204 | if (error_code ec = (*unwrap(RI))->getAddress(ret)) |
| 205 | report_fatal_error(ec.message()); |
| 206 | return ret; |
| 207 | } |
| 208 | |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 209 | uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) { |
| 210 | uint64_t ret; |
| 211 | if (error_code ec = (*unwrap(RI))->getOffset(ret)) |
| 212 | report_fatal_error(ec.message()); |
| 213 | return ret; |
| 214 | } |
| 215 | |
Owen Anderson | f7a89d0 | 2011-10-27 17:32:36 +0000 | [diff] [blame] | 216 | LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) { |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 217 | symbol_iterator ret = (*unwrap(RI))->getSymbol(); |
Owen Anderson | f7a89d0 | 2011-10-27 17:32:36 +0000 | [diff] [blame] | 218 | return wrap(new symbol_iterator(ret)); |
| 219 | } |
| 220 | |
| 221 | uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) { |
| 222 | uint64_t ret; |
| 223 | if (error_code ec = (*unwrap(RI))->getType(ret)) |
| 224 | report_fatal_error(ec.message()); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | // NOTE: Caller takes ownership of returned string. |
| 229 | const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) { |
| 230 | SmallVector<char, 0> ret; |
| 231 | if (error_code ec = (*unwrap(RI))->getTypeName(ret)) |
| 232 | report_fatal_error(ec.message()); |
| 233 | |
| 234 | char *str = static_cast<char*>(malloc(ret.size())); |
| 235 | std::copy(ret.begin(), ret.end(), str); |
| 236 | return str; |
| 237 | } |
| 238 | |
| 239 | // NOTE: Caller takes ownership of returned string. |
| 240 | const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) { |
| 241 | SmallVector<char, 0> ret; |
| 242 | if (error_code ec = (*unwrap(RI))->getValueString(ret)) |
| 243 | report_fatal_error(ec.message()); |
| 244 | |
| 245 | char *str = static_cast<char*>(malloc(ret.size())); |
| 246 | std::copy(ret.begin(), ret.end(), str); |
| 247 | return str; |
| 248 | } |
| 249 | |