ELFWriter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. //===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
  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 implements the target-independent ELF writer. This file writes out
  11. // the ELF file in the following order:
  12. //
  13. // #1. ELF Header
  14. // #2. '.text' section
  15. // #3. '.data' section
  16. // #4. '.bss' section (conceptual position in file)
  17. // ...
  18. // #X. '.shstrtab' section
  19. // #Y. Section Table
  20. //
  21. // The entries in the section table are laid out as:
  22. // #0. Null entry [required]
  23. // #1. ".text" entry - the program code
  24. // #2. ".data" entry - global variables with initializers. [ if needed ]
  25. // #3. ".bss" entry - global variables without initializers. [ if needed ]
  26. // ...
  27. // #N. ".shstrtab" entry - String table for the section names.
  28. //
  29. // NOTE: This code should eventually be extended to support 64-bit ELF (this
  30. // won't be hard), but we haven't done so yet!
  31. //
  32. //===----------------------------------------------------------------------===//
  33. #include "ELFWriter.h"
  34. #include "llvm/Module.h"
  35. #include "llvm/PassManager.h"
  36. #include "llvm/CodeGen/FileWriters.h"
  37. #include "llvm/CodeGen/MachineCodeEmitter.h"
  38. #include "llvm/CodeGen/MachineConstantPool.h"
  39. #include "llvm/CodeGen/MachineFunctionPass.h"
  40. #include "llvm/Target/TargetData.h"
  41. #include "llvm/Target/TargetELFWriterInfo.h"
  42. #include "llvm/Target/TargetMachine.h"
  43. #include "llvm/Support/Mangler.h"
  44. #include "llvm/Support/OutputBuffer.h"
  45. #include "llvm/Support/Streams.h"
  46. #include "llvm/Support/raw_ostream.h"
  47. #include <list>
  48. using namespace llvm;
  49. char ELFWriter::ID = 0;
  50. /// AddELFWriter - Concrete function to add the ELF writer to the function pass
  51. /// manager.
  52. MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
  53. raw_ostream &O,
  54. TargetMachine &TM) {
  55. ELFWriter *EW = new ELFWriter(O, TM);
  56. PM.add(EW);
  57. return &EW->getMachineCodeEmitter();
  58. }
  59. //===----------------------------------------------------------------------===//
  60. // ELFCodeEmitter Implementation
  61. //===----------------------------------------------------------------------===//
  62. namespace llvm {
  63. /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
  64. /// functions to the ELF file.
  65. class ELFCodeEmitter : public MachineCodeEmitter {
  66. ELFWriter &EW;
  67. TargetMachine &TM;
  68. ELFWriter::ELFSection *ES; // Section to write to.
  69. std::vector<unsigned char> *OutBuffer;
  70. size_t FnStart;
  71. public:
  72. explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
  73. void startFunction(MachineFunction &F);
  74. bool finishFunction(MachineFunction &F);
  75. void addRelocation(const MachineRelocation &MR) {
  76. assert(0 && "relo not handled yet!");
  77. }
  78. virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
  79. }
  80. virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
  81. assert(0 && "CP not implementated yet!");
  82. return 0;
  83. }
  84. virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
  85. assert(0 && "JT not implementated yet!");
  86. return 0;
  87. }
  88. virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
  89. assert(0 && "JT not implementated yet!");
  90. return 0;
  91. }
  92. virtual intptr_t getLabelAddress(uint64_t Label) const {
  93. assert(0 && "Label address not implementated yet!");
  94. abort();
  95. return 0;
  96. }
  97. virtual void emitLabel(uint64_t LabelID) {
  98. assert(0 && "emit Label not implementated yet!");
  99. abort();
  100. }
  101. virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
  102. /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
  103. void startFunctionStub(const GlobalValue* F, unsigned StubSize,
  104. unsigned Alignment = 1) {
  105. assert(0 && "JIT specific function called!");
  106. abort();
  107. }
  108. void *finishFunctionStub(const GlobalValue *F) {
  109. assert(0 && "JIT specific function called!");
  110. abort();
  111. return 0;
  112. }
  113. };
  114. }
  115. /// startFunction - This callback is invoked when a new machine function is
  116. /// about to be emitted.
  117. void ELFCodeEmitter::startFunction(MachineFunction &F) {
  118. // Align the output buffer to the appropriate alignment.
  119. unsigned Align = 16; // FIXME: GENERICIZE!!
  120. // Get the ELF Section that this function belongs in.
  121. ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
  122. ELFWriter::ELFSection::SHF_EXECINSTR |
  123. ELFWriter::ELFSection::SHF_ALLOC);
  124. OutBuffer = &ES->SectionData;
  125. cerr << "FIXME: This code needs to be updated for changes in the "
  126. << "CodeEmitter interfaces. In particular, this should set "
  127. << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
  128. abort();
  129. // Upgrade the section alignment if required.
  130. if (ES->Align < Align) ES->Align = Align;
  131. // Add padding zeros to the end of the buffer to make sure that the
  132. // function will start on the correct byte alignment within the section.
  133. OutputBuffer OB(*OutBuffer,
  134. TM.getTargetData()->getPointerSizeInBits() == 64,
  135. TM.getTargetData()->isLittleEndian());
  136. OB.align(Align);
  137. FnStart = OutBuffer->size();
  138. }
  139. /// finishFunction - This callback is invoked after the function is completely
  140. /// finished.
  141. bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
  142. // We now know the size of the function, add a symbol to represent it.
  143. ELFWriter::ELFSym FnSym(F.getFunction());
  144. // Figure out the binding (linkage) of the symbol.
  145. switch (F.getFunction()->getLinkage()) {
  146. default:
  147. // appending linkage is illegal for functions.
  148. assert(0 && "Unknown linkage type!");
  149. case GlobalValue::ExternalLinkage:
  150. FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
  151. break;
  152. case GlobalValue::LinkOnceLinkage:
  153. case GlobalValue::WeakLinkage:
  154. FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
  155. break;
  156. case GlobalValue::InternalLinkage:
  157. FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
  158. break;
  159. }
  160. ES->Size = OutBuffer->size();
  161. FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
  162. FnSym.SectionIdx = ES->SectionIdx;
  163. FnSym.Value = FnStart; // Value = Offset from start of Section.
  164. FnSym.Size = OutBuffer->size()-FnStart;
  165. // Finally, add it to the symtab.
  166. EW.SymbolTable.push_back(FnSym);
  167. return false;
  168. }
  169. //===----------------------------------------------------------------------===//
  170. // ELFWriter Implementation
  171. //===----------------------------------------------------------------------===//
  172. ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm)
  173. : MachineFunctionPass((intptr_t)&ID), O(o), TM(tm) {
  174. e_flags = 0; // e_flags defaults to 0, no flags.
  175. is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
  176. isLittleEndian = TM.getTargetData()->isLittleEndian();
  177. // Create the machine code emitter object for this target.
  178. MCE = new ELFCodeEmitter(*this);
  179. NumSections = 0;
  180. }
  181. ELFWriter::~ELFWriter() {
  182. delete MCE;
  183. }
  184. // doInitialization - Emit the file header and all of the global variables for
  185. // the module to the ELF file.
  186. bool ELFWriter::doInitialization(Module &M) {
  187. Mang = new Mangler(M);
  188. // Local alias to shortenify coming code.
  189. std::vector<unsigned char> &FH = FileHeader;
  190. OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
  191. FHOut.outbyte(0x7F); // EI_MAG0
  192. FHOut.outbyte('E'); // EI_MAG1
  193. FHOut.outbyte('L'); // EI_MAG2
  194. FHOut.outbyte('F'); // EI_MAG3
  195. FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
  196. FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
  197. FHOut.outbyte(1); // EI_VERSION
  198. FH.resize(16); // EI_PAD up to 16 bytes.
  199. // This should change for shared objects.
  200. FHOut.outhalf(1); // e_type = ET_REL
  201. FHOut.outhalf(TM.getELFWriterInfo()->getEMachine()); // target-defined
  202. FHOut.outword(1); // e_version = 1
  203. FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
  204. FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
  205. ELFHeader_e_shoff_Offset = FH.size();
  206. FHOut.outaddr(0); // e_shoff
  207. FHOut.outword(e_flags); // e_flags = whatever the target wants
  208. FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
  209. FHOut.outhalf(0); // e_phentsize = prog header entry size
  210. FHOut.outhalf(0); // e_phnum = # prog header entries = 0
  211. FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
  212. ELFHeader_e_shnum_Offset = FH.size();
  213. FHOut.outhalf(0); // e_shnum = # of section header ents
  214. ELFHeader_e_shstrndx_Offset = FH.size();
  215. FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
  216. // Add the null section, which is required to be first in the file.
  217. getSection("", 0, 0);
  218. // Start up the symbol table. The first entry in the symtab is the null
  219. // entry.
  220. SymbolTable.push_back(ELFSym(0));
  221. return false;
  222. }
  223. void ELFWriter::EmitGlobal(GlobalVariable *GV) {
  224. // If this is an external global, emit it now. TODO: Note that it would be
  225. // better to ignore the symbol here and only add it to the symbol table if
  226. // referenced.
  227. if (!GV->hasInitializer()) {
  228. ELFSym ExternalSym(GV);
  229. ExternalSym.SetBind(ELFSym::STB_GLOBAL);
  230. ExternalSym.SetType(ELFSym::STT_NOTYPE);
  231. ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
  232. SymbolTable.push_back(ExternalSym);
  233. return;
  234. }
  235. const Type *GVType = (const Type*)GV->getType();
  236. unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
  237. unsigned Size = TM.getTargetData()->getABITypeSize(GVType);
  238. // If this global has a zero initializer, it is part of the .bss or common
  239. // section.
  240. if (GV->getInitializer()->isNullValue()) {
  241. // If this global is part of the common block, add it now. Variables are
  242. // part of the common block if they are zero initialized and allowed to be
  243. // merged with other symbols.
  244. if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
  245. GV->hasCommonLinkage()) {
  246. ELFSym CommonSym(GV);
  247. // Value for common symbols is the alignment required.
  248. CommonSym.Value = Align;
  249. CommonSym.Size = Size;
  250. CommonSym.SetBind(ELFSym::STB_GLOBAL);
  251. CommonSym.SetType(ELFSym::STT_OBJECT);
  252. // TODO SOMEDAY: add ELF visibility.
  253. CommonSym.SectionIdx = ELFSection::SHN_COMMON;
  254. SymbolTable.push_back(CommonSym);
  255. return;
  256. }
  257. // Otherwise, this symbol is part of the .bss section. Emit it now.
  258. // Handle alignment. Ensure section is aligned at least as much as required
  259. // by this symbol.
  260. ELFSection &BSSSection = getBSSSection();
  261. BSSSection.Align = std::max(BSSSection.Align, Align);
  262. // Within the section, emit enough virtual padding to get us to an alignment
  263. // boundary.
  264. if (Align)
  265. BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
  266. ELFSym BSSSym(GV);
  267. BSSSym.Value = BSSSection.Size;
  268. BSSSym.Size = Size;
  269. BSSSym.SetType(ELFSym::STT_OBJECT);
  270. switch (GV->getLinkage()) {
  271. default: // weak/linkonce/common handled above
  272. assert(0 && "Unexpected linkage type!");
  273. case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
  274. case GlobalValue::ExternalLinkage:
  275. BSSSym.SetBind(ELFSym::STB_GLOBAL);
  276. break;
  277. case GlobalValue::InternalLinkage:
  278. BSSSym.SetBind(ELFSym::STB_LOCAL);
  279. break;
  280. }
  281. // Set the idx of the .bss section
  282. BSSSym.SectionIdx = BSSSection.SectionIdx;
  283. SymbolTable.push_back(BSSSym);
  284. // Reserve space in the .bss section for this symbol.
  285. BSSSection.Size += Size;
  286. return;
  287. }
  288. // FIXME: handle .rodata
  289. //assert(!GV->isConstant() && "unimp");
  290. // FIXME: handle .data
  291. //assert(0 && "unimp");
  292. }
  293. bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
  294. // Nothing to do here, this is all done through the MCE object above.
  295. return false;
  296. }
  297. /// doFinalization - Now that the module has been completely processed, emit
  298. /// the ELF file to 'O'.
  299. bool ELFWriter::doFinalization(Module &M) {
  300. // Okay, the ELF header and .text sections have been completed, build the
  301. // .data, .bss, and "common" sections next.
  302. for (Module::global_iterator I = M.global_begin(), E = M.global_end();
  303. I != E; ++I)
  304. EmitGlobal(I);
  305. // Emit the symbol table now, if non-empty.
  306. EmitSymbolTable();
  307. // FIXME: Emit the relocations now.
  308. // Emit the string table for the sections in the ELF file we have.
  309. EmitSectionTableStringTable();
  310. // Emit the sections to the .o file, and emit the section table for the file.
  311. OutputSectionsAndSectionTable();
  312. // We are done with the abstract symbols.
  313. SectionList.clear();
  314. NumSections = 0;
  315. // Release the name mangler object.
  316. delete Mang; Mang = 0;
  317. return false;
  318. }
  319. /// EmitSymbolTable - If the current symbol table is non-empty, emit the string
  320. /// table for it and then the symbol table itself.
  321. void ELFWriter::EmitSymbolTable() {
  322. if (SymbolTable.size() == 1) return; // Only the null entry.
  323. // FIXME: compact all local symbols to the start of the symtab.
  324. unsigned FirstNonLocalSymbol = 1;
  325. ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
  326. StrTab.Align = 1;
  327. DataBuffer &StrTabBuf = StrTab.SectionData;
  328. OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
  329. // Set the zero'th symbol to a null byte, as required.
  330. StrTabOut.outbyte(0);
  331. SymbolTable[0].NameIdx = 0;
  332. unsigned Index = 1;
  333. for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
  334. // Use the name mangler to uniquify the LLVM symbol.
  335. std::string Name = Mang->getValueName(SymbolTable[i].GV);
  336. if (Name.empty()) {
  337. SymbolTable[i].NameIdx = 0;
  338. } else {
  339. SymbolTable[i].NameIdx = Index;
  340. // Add the name to the output buffer, including the null terminator.
  341. StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
  342. // Add a null terminator.
  343. StrTabBuf.push_back(0);
  344. // Keep track of the number of bytes emitted to this section.
  345. Index += Name.size()+1;
  346. }
  347. }
  348. assert(Index == StrTabBuf.size());
  349. StrTab.Size = Index;
  350. // Now that we have emitted the string table and know the offset into the
  351. // string table of each symbol, emit the symbol table itself.
  352. ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
  353. SymTab.Align = is64Bit ? 8 : 4;
  354. SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
  355. SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
  356. SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
  357. DataBuffer &SymTabBuf = SymTab.SectionData;
  358. OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
  359. if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
  360. for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
  361. ELFSym &Sym = SymbolTable[i];
  362. SymTabOut.outword(Sym.NameIdx);
  363. SymTabOut.outaddr32(Sym.Value);
  364. SymTabOut.outword(Sym.Size);
  365. SymTabOut.outbyte(Sym.Info);
  366. SymTabOut.outbyte(Sym.Other);
  367. SymTabOut.outhalf(Sym.SectionIdx);
  368. }
  369. } else {
  370. for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
  371. ELFSym &Sym = SymbolTable[i];
  372. SymTabOut.outword(Sym.NameIdx);
  373. SymTabOut.outbyte(Sym.Info);
  374. SymTabOut.outbyte(Sym.Other);
  375. SymTabOut.outhalf(Sym.SectionIdx);
  376. SymTabOut.outaddr64(Sym.Value);
  377. SymTabOut.outxword(Sym.Size);
  378. }
  379. }
  380. SymTab.Size = SymTabBuf.size();
  381. }
  382. /// EmitSectionTableStringTable - This method adds and emits a section for the
  383. /// ELF Section Table string table: the string table that holds all of the
  384. /// section names.
  385. void ELFWriter::EmitSectionTableStringTable() {
  386. // First step: add the section for the string table to the list of sections:
  387. ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
  388. // Now that we know which section number is the .shstrtab section, update the
  389. // e_shstrndx entry in the ELF header.
  390. OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
  391. FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
  392. // Set the NameIdx of each section in the string table and emit the bytes for
  393. // the string table.
  394. unsigned Index = 0;
  395. DataBuffer &Buf = SHStrTab.SectionData;
  396. for (std::list<ELFSection>::iterator I = SectionList.begin(),
  397. E = SectionList.end(); I != E; ++I) {
  398. // Set the index into the table. Note if we have lots of entries with
  399. // common suffixes, we could memoize them here if we cared.
  400. I->NameIdx = Index;
  401. // Add the name to the output buffer, including the null terminator.
  402. Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
  403. // Add a null terminator.
  404. Buf.push_back(0);
  405. // Keep track of the number of bytes emitted to this section.
  406. Index += I->Name.size()+1;
  407. }
  408. // Set the size of .shstrtab now that we know what it is.
  409. assert(Index == Buf.size());
  410. SHStrTab.Size = Index;
  411. }
  412. /// OutputSectionsAndSectionTable - Now that we have constructed the file header
  413. /// and all of the sections, emit these to the ostream destination and emit the
  414. /// SectionTable.
  415. void ELFWriter::OutputSectionsAndSectionTable() {
  416. // Pass #1: Compute the file offset for each section.
  417. size_t FileOff = FileHeader.size(); // File header first.
  418. // Emit all of the section data in order.
  419. for (std::list<ELFSection>::iterator I = SectionList.begin(),
  420. E = SectionList.end(); I != E; ++I) {
  421. // Align FileOff to whatever the alignment restrictions of the section are.
  422. if (I->Align)
  423. FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
  424. I->Offset = FileOff;
  425. FileOff += I->SectionData.size();
  426. }
  427. // Align Section Header.
  428. unsigned TableAlign = is64Bit ? 8 : 4;
  429. FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
  430. // Now that we know where all of the sections will be emitted, set the e_shnum
  431. // entry in the ELF header.
  432. OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
  433. FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
  434. // Now that we know the offset in the file of the section table, update the
  435. // e_shoff address in the ELF header.
  436. FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
  437. // Now that we know all of the data in the file header, emit it and all of the
  438. // sections!
  439. O.write((char*)&FileHeader[0], FileHeader.size());
  440. FileOff = FileHeader.size();
  441. DataBuffer().swap(FileHeader);
  442. DataBuffer Table;
  443. OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
  444. // Emit all of the section data and build the section table itself.
  445. while (!SectionList.empty()) {
  446. const ELFSection &S = *SectionList.begin();
  447. // Align FileOff to whatever the alignment restrictions of the section are.
  448. if (S.Align)
  449. for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
  450. FileOff != NewFileOff; ++FileOff)
  451. O << (char)0xAB;
  452. O.write((char*)&S.SectionData[0], S.SectionData.size());
  453. FileOff += S.SectionData.size();
  454. TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
  455. TableOut.outword(S.Type); // sh_type - Section contents & semantics
  456. TableOut.outword(S.Flags); // sh_flags - Section flags.
  457. TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
  458. TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
  459. TableOut.outword(S.Size); // sh_size - The section size.
  460. TableOut.outword(S.Link); // sh_link - Section header table index link.
  461. TableOut.outword(S.Info); // sh_info - Auxillary information.
  462. TableOut.outword(S.Align); // sh_addralign - Alignment of section.
  463. TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
  464. SectionList.pop_front();
  465. }
  466. // Align output for the section table.
  467. for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
  468. FileOff != NewFileOff; ++FileOff)
  469. O << (char)0xAB;
  470. // Emit the section table itself.
  471. O.write((char*)&Table[0], Table.size());
  472. }