LLVM API Documentation
00001 //=== MachO.h - Mach-O structures and constants -----------------*- 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 MachO . 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef MACHO_H 00015 #define MACHO_H 00016 00017 #include "llvm/CodeGen/BinaryObject.h" 00018 #include <string> 00019 #include <vector> 00020 00021 namespace llvm { 00022 00023 class GlobalValue; 00024 class TargetAsmInfo; 00025 00026 /// MachOSym - This struct contains information about each symbol that is 00027 /// added to logical symbol table for the module. This is eventually 00028 /// turned into a real symbol table in the file. 00029 struct MachOSym { 00030 const GlobalValue *GV; // The global value this corresponds to. 00031 std::string GVName; // The mangled name of the global value. 00032 uint32_t n_strx; // index into the string table 00033 uint8_t n_type; // type flag 00034 uint8_t n_sect; // section number or NO_SECT 00035 int16_t n_desc; // see <mach-o/stab.h> 00036 uint64_t n_value; // value for this symbol (or stab offset) 00037 00038 // Constants for the n_sect field 00039 // see <mach-o/nlist.h> 00040 enum { NO_SECT = 0 }; // symbol is not in any section 00041 00042 // Constants for the n_type field 00043 // see <mach-o/nlist.h> 00044 enum { N_UNDF = 0x0, // undefined, n_sect == NO_SECT 00045 N_ABS = 0x2, // absolute, n_sect == NO_SECT 00046 N_SECT = 0xe, // defined in section number n_sect 00047 N_PBUD = 0xc, // prebound undefined (defined in a dylib) 00048 N_INDR = 0xa // indirect 00049 }; 00050 // The following bits are OR'd into the types above. For example, a type 00051 // of 0x0f would be an external N_SECT symbol (0x0e | 0x01). 00052 enum { N_EXT = 0x01, // external symbol bit 00053 N_PEXT = 0x10 // private external symbol bit 00054 }; 00055 00056 // Constants for the n_desc field 00057 // see <mach-o/loader.h> 00058 enum { REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0, 00059 REFERENCE_FLAG_UNDEFINED_LAZY = 1, 00060 REFERENCE_FLAG_DEFINED = 2, 00061 REFERENCE_FLAG_PRIVATE_DEFINED = 3, 00062 REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4, 00063 REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5 00064 }; 00065 enum { N_NO_DEAD_STRIP = 0x0020, // symbol is not to be dead stripped 00066 N_WEAK_REF = 0x0040, // symbol is weak referenced 00067 N_WEAK_DEF = 0x0080 // coalesced symbol is a weak definition 00068 }; 00069 00070 MachOSym(const GlobalValue *gv, std::string name, uint8_t sect, 00071 const TargetAsmInfo *TAI); 00072 00073 struct SymCmp { 00074 // FIXME: this does not appear to be sorting 'f' after 'F' 00075 bool operator()(const MachOSym &LHS, const MachOSym &RHS) { 00076 return LHS.GVName < RHS.GVName; 00077 } 00078 }; 00079 00080 00081 /// PartitionByLocal - Simple boolean predicate that returns true if Sym is 00082 /// a local symbol rather than an external symbol. 00083 00084 static inline bool PartitionByLocal(const MachOSym &Sym) { 00085 return (Sym.n_type & (MachOSym::N_EXT | MachOSym::N_PEXT)) == 0; 00086 } 00087 00088 /// PartitionByDefined - Simple boolean predicate that returns true if Sym is 00089 /// defined in this module. 00090 00091 static inline bool PartitionByDefined(const MachOSym &Sym) { 00092 // FIXME: Do N_ABS or N_INDR count as defined? 00093 return (Sym.n_type & MachOSym::N_SECT) == MachOSym::N_SECT; 00094 } 00095 00096 }; // end struct MachOSym 00097 00098 /// MachOHeader - This struct contains the header information about a 00099 /// specific architecture type/subtype pair that is emitted to the file. 00100 00101 struct MachOHeader { 00102 uint32_t magic; // mach magic number identifier 00103 uint32_t filetype; // type of file 00104 uint32_t ncmds; // number of load commands 00105 uint32_t sizeofcmds; // the size of all the load commands 00106 uint32_t flags; // flags 00107 uint32_t reserved; // 64-bit only 00108 00109 /// HeaderData - The actual data for the header which we are building 00110 /// up for emission to the file. 00111 std::vector<unsigned char> HeaderData; 00112 00113 // Constants for the filetype field 00114 // see <mach-o/loader.h> for additional info on the various types 00115 enum { MH_OBJECT = 1, // relocatable object file 00116 MH_EXECUTE = 2, // demand paged executable file 00117 MH_FVMLIB = 3, // fixed VM shared library file 00118 MH_CORE = 4, // core file 00119 MH_PRELOAD = 5, // preloaded executable file 00120 MH_DYLIB = 6, // dynamically bound shared library 00121 MH_DYLINKER = 7, // dynamic link editor 00122 MH_BUNDLE = 8, // dynamically bound bundle file 00123 MH_DYLIB_STUB = 9, // shared library stub for static linking only 00124 MH_DSYM = 10 // companion file wiht only debug sections 00125 }; 00126 00127 // Constants for the flags field 00128 enum { MH_NOUNDEFS = 1 << 0, 00129 // the object file has no undefined references 00130 MH_INCRLINK = 1 << 1, 00131 // the object file is the output of an incremental link against 00132 // a base file and cannot be link edited again 00133 MH_DYLDLINK = 1 << 2, 00134 // the object file is input for the dynamic linker and cannot be 00135 // statically link edited again. 00136 MH_BINDATLOAD = 1 << 3, 00137 // the object file's undefined references are bound by the 00138 // dynamic linker when loaded. 00139 MH_PREBOUND = 1 << 4, 00140 // the file has its dynamic undefined references prebound 00141 MH_SPLIT_SEGS = 1 << 5, 00142 // the file has its read-only and read-write segments split 00143 // see <mach/shared_memory_server.h> 00144 MH_LAZY_INIT = 1 << 6, 00145 // the shared library init routine is to be run lazily via 00146 // catching memory faults to its writable segments (obsolete) 00147 MH_TWOLEVEL = 1 << 7, 00148 // the image is using two-level namespace bindings 00149 MH_FORCE_FLAT = 1 << 8, 00150 // the executable is forcing all images to use flat namespace 00151 // bindings. 00152 MH_NOMULTIDEFS = 1 << 8, 00153 // this umbrella guarantees no multiple definitions of symbols 00154 // in its sub-images so the two-level namespace hints can 00155 // always be used. 00156 MH_NOFIXPREBINDING = 1 << 10, 00157 // do not have dyld notify the prebidning agent about this 00158 // executable. 00159 MH_PREBINDABLE = 1 << 11, 00160 // the binary is not prebound but can have its prebinding 00161 // redone. only used when MH_PREBOUND is not set. 00162 MH_ALLMODSBOUND = 1 << 12, 00163 // indicates that this binary binds to all two-level namespace 00164 // modules of its dependent libraries. Only used when 00165 // MH_PREBINDABLE and MH_TWOLEVEL are both set. 00166 MH_SUBSECTIONS_VIA_SYMBOLS = 1 << 13, 00167 // safe to divide up the sections into sub-sections via symbols 00168 // for dead code stripping. 00169 MH_CANONICAL = 1 << 14, 00170 // the binary has been canonicalized via the unprebind operation 00171 MH_WEAK_DEFINES = 1 << 15, 00172 // the final linked image contains external weak symbols 00173 MH_BINDS_TO_WEAK = 1 << 16, 00174 // the final linked image uses weak symbols 00175 MH_ALLOW_STACK_EXECUTION = 1 << 17 00176 // When this bit is set, all stacks in the task will be given 00177 // stack execution privilege. Only used in MH_EXECUTE filetype 00178 }; 00179 00180 MachOHeader() : magic(0), filetype(0), ncmds(0), sizeofcmds(0), flags(0), 00181 reserved(0) {} 00182 00183 /// cmdSize - This routine returns the size of the MachOSection as written 00184 /// to disk, depending on whether the destination is a 64 bit Mach-O file. 00185 unsigned cmdSize(bool is64Bit) const { 00186 if (is64Bit) 00187 return 8 * sizeof(uint32_t); 00188 else 00189 return 7 * sizeof(uint32_t); 00190 } 00191 00192 /// setMagic - This routine sets the appropriate value for the 'magic' 00193 /// field based on pointer size and endianness. 00194 void setMagic(bool isLittleEndian, bool is64Bit) { 00195 if (isLittleEndian) 00196 if (is64Bit) magic = 0xcffaedfe; 00197 else magic = 0xcefaedfe; 00198 else 00199 if (is64Bit) magic = 0xfeedfacf; 00200 else magic = 0xfeedface; 00201 } 00202 00203 }; // end struct MachOHeader 00204 00205 /// MachOSegment - This struct contains the necessary information to 00206 /// emit the load commands for each section in the file. 00207 struct MachOSegment { 00208 uint32_t cmd; // LC_SEGMENT or LC_SEGMENT_64 00209 uint32_t cmdsize; // Total size of this struct and section commands 00210 std::string segname; // segment name 00211 uint64_t vmaddr; // address of this segment 00212 uint64_t vmsize; // size of this segment, may be larger than filesize 00213 uint64_t fileoff; // offset in file 00214 uint64_t filesize; // amount to read from file 00215 uint32_t maxprot; // maximum VM protection 00216 uint32_t initprot; // initial VM protection 00217 uint32_t nsects; // number of sections in this segment 00218 uint32_t flags; // flags 00219 00220 // The following constants are getting pulled in by one of the 00221 // system headers, which creates a neat clash with the enum. 00222 #if !defined(VM_PROT_NONE) 00223 #define VM_PROT_NONE 0x00 00224 #endif 00225 #if !defined(VM_PROT_READ) 00226 #define VM_PROT_READ 0x01 00227 #endif 00228 #if !defined(VM_PROT_WRITE) 00229 #define VM_PROT_WRITE 0x02 00230 #endif 00231 #if !defined(VM_PROT_EXECUTE) 00232 #define VM_PROT_EXECUTE 0x04 00233 #endif 00234 #if !defined(VM_PROT_ALL) 00235 #define VM_PROT_ALL 0x07 00236 #endif 00237 00238 // Constants for the vm protection fields 00239 // see <mach-o/vm_prot.h> 00240 enum { SEG_VM_PROT_NONE = VM_PROT_NONE, 00241 SEG_VM_PROT_READ = VM_PROT_READ, // read permission 00242 SEG_VM_PROT_WRITE = VM_PROT_WRITE, // write permission 00243 SEG_VM_PROT_EXECUTE = VM_PROT_EXECUTE, 00244 SEG_VM_PROT_ALL = VM_PROT_ALL 00245 }; 00246 00247 // Constants for the cmd field 00248 // see <mach-o/loader.h> 00249 enum { LC_SEGMENT = 0x01, // segment of this file to be mapped 00250 LC_SEGMENT_64 = 0x19 // 64-bit segment of this file to be mapped 00251 }; 00252 00253 /// cmdSize - This routine returns the size of the MachOSection as written 00254 /// to disk, depending on whether the destination is a 64 bit Mach-O file. 00255 unsigned cmdSize(bool is64Bit) const { 00256 if (is64Bit) 00257 return 6 * sizeof(uint32_t) + 4 * sizeof(uint64_t) + 16; 00258 else 00259 return 10 * sizeof(uint32_t) + 16; // addresses only 32 bits 00260 } 00261 00262 MachOSegment(const std::string &seg, bool is64Bit) 00263 : cmd(is64Bit ? LC_SEGMENT_64 : LC_SEGMENT), cmdsize(0), segname(seg), 00264 vmaddr(0), vmsize(0), fileoff(0), filesize(0), maxprot(VM_PROT_ALL), 00265 initprot(VM_PROT_ALL), nsects(0), flags(0) { } 00266 }; 00267 00268 /// MachOSection - This struct contains information about each section in a 00269 /// particular segment that is emitted to the file. This is eventually 00270 /// turned into the SectionCommand in the load command for a particlar 00271 /// segment. 00272 00273 struct MachOSection : public BinaryObject { 00274 std::string sectname; // name of this section, 00275 std::string segname; // segment this section goes in 00276 uint64_t addr; // memory address of this section 00277 uint32_t offset; // file offset of this section 00278 uint32_t align; // section alignment (power of 2) 00279 uint32_t reloff; // file offset of relocation entries 00280 uint32_t nreloc; // number of relocation entries 00281 uint32_t flags; // flags (section type and attributes) 00282 uint32_t reserved1; // reserved (for offset or index) 00283 uint32_t reserved2; // reserved (for count or sizeof) 00284 uint32_t reserved3; // reserved (64 bit only) 00285 00286 /// A unique number for this section, which will be used to match symbols 00287 /// to the correct section. 00288 uint32_t Index; 00289 00290 /// RelocBuffer - A buffer to hold the mach-o relocations before we write 00291 /// them out at the appropriate location in the file. 00292 std::vector<unsigned char> RelocBuffer; 00293 00294 // Constants for the section types (low 8 bits of flags field) 00295 // see <mach-o/loader.h> 00296 enum { S_REGULAR = 0, 00297 // regular section 00298 S_ZEROFILL = 1, 00299 // zero fill on demand section 00300 S_CSTRING_LITERALS = 2, 00301 // section with only literal C strings 00302 S_4BYTE_LITERALS = 3, 00303 // section with only 4 byte literals 00304 S_8BYTE_LITERALS = 4, 00305 // section with only 8 byte literals 00306 S_LITERAL_POINTERS = 5, 00307 // section with only pointers to literals 00308 S_NON_LAZY_SYMBOL_POINTERS = 6, 00309 // section with only non-lazy symbol pointers 00310 S_LAZY_SYMBOL_POINTERS = 7, 00311 // section with only lazy symbol pointers 00312 S_SYMBOL_STUBS = 8, 00313 // section with only symbol stubs 00314 // byte size of stub in the reserved2 field 00315 S_MOD_INIT_FUNC_POINTERS = 9, 00316 // section with only function pointers for initialization 00317 S_MOD_TERM_FUNC_POINTERS = 10, 00318 // section with only function pointers for termination 00319 S_COALESCED = 11, 00320 // section contains symbols that are coalesced 00321 S_GB_ZEROFILL = 12, 00322 // zero fill on demand section (that can be larger than 4GB) 00323 S_INTERPOSING = 13, 00324 // section with only pairs of function pointers for interposing 00325 S_16BYTE_LITERALS = 14 00326 // section with only 16 byte literals 00327 }; 00328 00329 // Constants for the section flags (high 24 bits of flags field) 00330 // see <mach-o/loader.h> 00331 enum { S_ATTR_PURE_INSTRUCTIONS = 1 << 31, 00332 // section contains only true machine instructions 00333 S_ATTR_NO_TOC = 1 << 30, 00334 // section contains coalesced symbols that are not to be in a 00335 // ranlib table of contents 00336 S_ATTR_STRIP_STATIC_SYMS = 1 << 29, 00337 // ok to strip static symbols in this section in files with the 00338 // MY_DYLDLINK flag 00339 S_ATTR_NO_DEAD_STRIP = 1 << 28, 00340 // no dead stripping 00341 S_ATTR_LIVE_SUPPORT = 1 << 27, 00342 // blocks are live if they reference live blocks 00343 S_ATTR_SELF_MODIFYING_CODE = 1 << 26, 00344 // used with i386 code stubs written on by dyld 00345 S_ATTR_DEBUG = 1 << 25, 00346 // a debug section 00347 S_ATTR_SOME_INSTRUCTIONS = 1 << 10, 00348 // section contains some machine instructions 00349 S_ATTR_EXT_RELOC = 1 << 9, 00350 // section has external relocation entries 00351 S_ATTR_LOC_RELOC = 1 << 8 00352 // section has local relocation entries 00353 }; 00354 00355 /// cmdSize - This routine returns the size of the MachOSection as written 00356 /// to disk, depending on whether the destination is a 64 bit Mach-O file. 00357 unsigned cmdSize(bool is64Bit) const { 00358 if (is64Bit) 00359 return 7 * sizeof(uint32_t) + 2 * sizeof(uint64_t) + 32; 00360 else 00361 return 9 * sizeof(uint32_t) + 32; // addresses only 32 bits 00362 } 00363 00364 MachOSection(const std::string &seg, const std::string §) 00365 : BinaryObject(), sectname(sect), segname(seg), addr(0), offset(0), 00366 align(2), reloff(0), nreloc(0), flags(0), reserved1(0), reserved2(0), 00367 reserved3(0) { } 00368 00369 }; // end struct MachOSection 00370 00371 /// MachOSymTab - This struct contains information about the offsets and 00372 /// size of symbol table information. 00373 /// segment. 00374 struct MachODySymTab { 00375 uint32_t cmd; // LC_DYSYMTAB 00376 uint32_t cmdsize; // sizeof(MachODySymTab) 00377 uint32_t ilocalsym; // index to local symbols 00378 uint32_t nlocalsym; // number of local symbols 00379 uint32_t iextdefsym; // index to externally defined symbols 00380 uint32_t nextdefsym; // number of externally defined symbols 00381 uint32_t iundefsym; // index to undefined symbols 00382 uint32_t nundefsym; // number of undefined symbols 00383 uint32_t tocoff; // file offset to table of contents 00384 uint32_t ntoc; // number of entries in table of contents 00385 uint32_t modtaboff; // file offset to module table 00386 uint32_t nmodtab; // number of module table entries 00387 uint32_t extrefsymoff; // offset to referenced symbol table 00388 uint32_t nextrefsyms; // number of referenced symbol table entries 00389 uint32_t indirectsymoff; // file offset to the indirect symbol table 00390 uint32_t nindirectsyms; // number of indirect symbol table entries 00391 uint32_t extreloff; // offset to external relocation entries 00392 uint32_t nextrel; // number of external relocation entries 00393 uint32_t locreloff; // offset to local relocation entries 00394 uint32_t nlocrel; // number of local relocation entries 00395 00396 // Constants for the cmd field 00397 // see <mach-o/loader.h> 00398 enum { LC_DYSYMTAB = 0x0B // dynamic link-edit symbol table info 00399 }; 00400 00401 MachODySymTab() : cmd(LC_DYSYMTAB), cmdsize(20 * sizeof(uint32_t)), 00402 ilocalsym(0), nlocalsym(0), iextdefsym(0), nextdefsym(0), 00403 iundefsym(0), nundefsym(0), tocoff(0), ntoc(0), modtaboff(0), 00404 nmodtab(0), extrefsymoff(0), nextrefsyms(0), indirectsymoff(0), 00405 nindirectsyms(0), extreloff(0), nextrel(0), locreloff(0), nlocrel(0) {} 00406 00407 }; // end struct MachODySymTab 00408 00409 } // end namespace llvm 00410 00411 #endif 00412