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