LLVM API Documentation
00001 //===- Object.cpp - C bindings to the object file library--------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the C bindings to the file-format-independent object 00011 // library. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/ADT/SmallVector.h" 00016 #include "llvm-c/Object.h" 00017 #include "llvm/Object/ObjectFile.h" 00018 00019 using namespace llvm; 00020 using namespace object; 00021 00022 inline OwningBinary<ObjectFile> *unwrap(LLVMObjectFileRef OF) { 00023 return reinterpret_cast<OwningBinary<ObjectFile> *>(OF); 00024 } 00025 00026 inline LLVMObjectFileRef wrap(const OwningBinary<ObjectFile> *OF) { 00027 return reinterpret_cast<LLVMObjectFileRef>( 00028 const_cast<OwningBinary<ObjectFile> *>(OF)); 00029 } 00030 00031 inline section_iterator *unwrap(LLVMSectionIteratorRef SI) { 00032 return reinterpret_cast<section_iterator*>(SI); 00033 } 00034 00035 inline LLVMSectionIteratorRef 00036 wrap(const section_iterator *SI) { 00037 return reinterpret_cast<LLVMSectionIteratorRef> 00038 (const_cast<section_iterator*>(SI)); 00039 } 00040 00041 inline symbol_iterator *unwrap(LLVMSymbolIteratorRef SI) { 00042 return reinterpret_cast<symbol_iterator*>(SI); 00043 } 00044 00045 inline LLVMSymbolIteratorRef 00046 wrap(const symbol_iterator *SI) { 00047 return reinterpret_cast<LLVMSymbolIteratorRef> 00048 (const_cast<symbol_iterator*>(SI)); 00049 } 00050 00051 inline relocation_iterator *unwrap(LLVMRelocationIteratorRef SI) { 00052 return reinterpret_cast<relocation_iterator*>(SI); 00053 } 00054 00055 inline LLVMRelocationIteratorRef 00056 wrap(const relocation_iterator *SI) { 00057 return reinterpret_cast<LLVMRelocationIteratorRef> 00058 (const_cast<relocation_iterator*>(SI)); 00059 } 00060 00061 // ObjectFile creation 00062 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) { 00063 std::unique_ptr<MemoryBuffer> Buf(unwrap(MemBuf)); 00064 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr( 00065 ObjectFile::createObjectFile(Buf->getMemBufferRef())); 00066 std::unique_ptr<ObjectFile> Obj; 00067 if (!ObjOrErr) 00068 return nullptr; 00069 00070 auto *Ret = new OwningBinary<ObjectFile>(std::move(ObjOrErr.get()), std::move(Buf)); 00071 return wrap(Ret); 00072 } 00073 00074 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) { 00075 delete unwrap(ObjectFile); 00076 } 00077 00078 // ObjectFile Section iterators 00079 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef OF) { 00080 OwningBinary<ObjectFile> *OB = unwrap(OF); 00081 section_iterator SI = OB->getBinary()->section_begin(); 00082 return wrap(new section_iterator(SI)); 00083 } 00084 00085 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) { 00086 delete unwrap(SI); 00087 } 00088 00089 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef OF, 00090 LLVMSectionIteratorRef SI) { 00091 OwningBinary<ObjectFile> *OB = unwrap(OF); 00092 return (*unwrap(SI) == OB->getBinary()->section_end()) ? 1 : 0; 00093 } 00094 00095 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) { 00096 ++(*unwrap(SI)); 00097 } 00098 00099 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect, 00100 LLVMSymbolIteratorRef Sym) { 00101 if (std::error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect))) 00102 report_fatal_error(ec.message()); 00103 } 00104 00105 // ObjectFile Symbol iterators 00106 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef OF) { 00107 OwningBinary<ObjectFile> *OB = unwrap(OF); 00108 symbol_iterator SI = OB->getBinary()->symbol_begin(); 00109 return wrap(new symbol_iterator(SI)); 00110 } 00111 00112 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) { 00113 delete unwrap(SI); 00114 } 00115 00116 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef OF, 00117 LLVMSymbolIteratorRef SI) { 00118 OwningBinary<ObjectFile> *OB = unwrap(OF); 00119 return (*unwrap(SI) == OB->getBinary()->symbol_end()) ? 1 : 0; 00120 } 00121 00122 void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) { 00123 ++(*unwrap(SI)); 00124 } 00125 00126 // SectionRef accessors 00127 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) { 00128 StringRef ret; 00129 if (std::error_code ec = (*unwrap(SI))->getName(ret)) 00130 report_fatal_error(ec.message()); 00131 return ret.data(); 00132 } 00133 00134 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) { 00135 uint64_t ret; 00136 if (std::error_code ec = (*unwrap(SI))->getSize(ret)) 00137 report_fatal_error(ec.message()); 00138 return ret; 00139 } 00140 00141 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) { 00142 StringRef ret; 00143 if (std::error_code ec = (*unwrap(SI))->getContents(ret)) 00144 report_fatal_error(ec.message()); 00145 return ret.data(); 00146 } 00147 00148 uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) { 00149 uint64_t ret; 00150 if (std::error_code ec = (*unwrap(SI))->getAddress(ret)) 00151 report_fatal_error(ec.message()); 00152 return ret; 00153 } 00154 00155 LLVMBool LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI, 00156 LLVMSymbolIteratorRef Sym) { 00157 bool ret; 00158 if (std::error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret)) 00159 report_fatal_error(ec.message()); 00160 return ret; 00161 } 00162 00163 // Section Relocation iterators 00164 LLVMRelocationIteratorRef LLVMGetRelocations(LLVMSectionIteratorRef Section) { 00165 relocation_iterator SI = (*unwrap(Section))->relocation_begin(); 00166 return wrap(new relocation_iterator(SI)); 00167 } 00168 00169 void LLVMDisposeRelocationIterator(LLVMRelocationIteratorRef SI) { 00170 delete unwrap(SI); 00171 } 00172 00173 LLVMBool LLVMIsRelocationIteratorAtEnd(LLVMSectionIteratorRef Section, 00174 LLVMRelocationIteratorRef SI) { 00175 return (*unwrap(SI) == (*unwrap(Section))->relocation_end()) ? 1 : 0; 00176 } 00177 00178 void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef SI) { 00179 ++(*unwrap(SI)); 00180 } 00181 00182 00183 // SymbolRef accessors 00184 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) { 00185 StringRef ret; 00186 if (std::error_code ec = (*unwrap(SI))->getName(ret)) 00187 report_fatal_error(ec.message()); 00188 return ret.data(); 00189 } 00190 00191 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) { 00192 uint64_t ret; 00193 if (std::error_code ec = (*unwrap(SI))->getAddress(ret)) 00194 report_fatal_error(ec.message()); 00195 return ret; 00196 } 00197 00198 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) { 00199 uint64_t ret; 00200 if (std::error_code ec = (*unwrap(SI))->getSize(ret)) 00201 report_fatal_error(ec.message()); 00202 return ret; 00203 } 00204 00205 // RelocationRef accessors 00206 uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) { 00207 uint64_t ret; 00208 if (std::error_code ec = (*unwrap(RI))->getAddress(ret)) 00209 report_fatal_error(ec.message()); 00210 return ret; 00211 } 00212 00213 uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) { 00214 uint64_t ret; 00215 if (std::error_code ec = (*unwrap(RI))->getOffset(ret)) 00216 report_fatal_error(ec.message()); 00217 return ret; 00218 } 00219 00220 LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI) { 00221 symbol_iterator ret = (*unwrap(RI))->getSymbol(); 00222 return wrap(new symbol_iterator(ret)); 00223 } 00224 00225 uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) { 00226 uint64_t ret; 00227 if (std::error_code ec = (*unwrap(RI))->getType(ret)) 00228 report_fatal_error(ec.message()); 00229 return ret; 00230 } 00231 00232 // NOTE: Caller takes ownership of returned string. 00233 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) { 00234 SmallVector<char, 0> ret; 00235 if (std::error_code ec = (*unwrap(RI))->getTypeName(ret)) 00236 report_fatal_error(ec.message()); 00237 00238 char *str = static_cast<char*>(malloc(ret.size())); 00239 std::copy(ret.begin(), ret.end(), str); 00240 return str; 00241 } 00242 00243 // NOTE: Caller takes ownership of returned string. 00244 const char *LLVMGetRelocationValueString(LLVMRelocationIteratorRef RI) { 00245 SmallVector<char, 0> ret; 00246 if (std::error_code ec = (*unwrap(RI))->getValueString(ret)) 00247 report_fatal_error(ec.message()); 00248 00249 char *str = static_cast<char*>(malloc(ret.size())); 00250 std::copy(ret.begin(), ret.end(), str); 00251 return str; 00252 } 00253