| OLD | NEW |
| 1 //===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===// | 1 //===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the target-independent ELF writer. This file writes out | 10 // This file implements the target-independent ELF writer. This file writes out |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 is64Bit(TM.getTargetData()->getPointerSizeInBits() == 64), | 69 is64Bit(TM.getTargetData()->getPointerSizeInBits() == 64), |
| 70 isLittleEndian(TM.getTargetData()->isLittleEndian()), | 70 isLittleEndian(TM.getTargetData()->isLittleEndian()), |
| 71 ElfHdr(isLittleEndian, is64Bit) { | 71 ElfHdr(isLittleEndian, is64Bit) { |
| 72 | 72 |
| 73 MAI = TM.getMCAsmInfo(); | 73 MAI = TM.getMCAsmInfo(); |
| 74 TEW = TM.getELFWriterInfo(); | 74 TEW = TM.getELFWriterInfo(); |
| 75 | 75 |
| 76 // Create the object code emitter object for this target. | 76 // Create the object code emitter object for this target. |
| 77 ElfCE = new ELFCodeEmitter(*this); | 77 ElfCE = new ELFCodeEmitter(*this); |
| 78 | 78 |
| 79 // Create the arch-specific relocation helper |
| 80 TERH = TEW->getRelocHelper(); |
| 81 |
| 79 // Inital number of sections | 82 // Inital number of sections |
| 80 NumSections = 0; | 83 NumSections = 0; |
| 81 } | 84 } |
| 82 | 85 |
| 83 ELFWriter::~ELFWriter() { | 86 ELFWriter::~ELFWriter() { |
| 84 delete ElfCE; | 87 delete ElfCE; |
| 85 delete &OutContext; | 88 delete &OutContext; |
| 86 | 89 |
| 87 while(!SymbolList.empty()) { | 90 while(!SymbolList.empty()) { |
| 88 delete SymbolList.back(); | 91 delete SymbolList.back(); |
| (...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 | 717 |
| 715 // Dump the sections and section table to the .o file. | 718 // Dump the sections and section table to the .o file. |
| 716 OutputSectionsAndSectionTable(); | 719 OutputSectionsAndSectionTable(); |
| 717 | 720 |
| 718 return false; | 721 return false; |
| 719 } | 722 } |
| 720 | 723 |
| 721 // RelocateField - Patch relocatable field with 'Offset' in 'BO' | 724 // RelocateField - Patch relocatable field with 'Offset' in 'BO' |
| 722 // using a 'Value' of known 'Size' | 725 // using a 'Value' of known 'Size' |
| 723 void ELFWriter::RelocateField(BinaryObject &BO, uint32_t Offset, | 726 void ELFWriter::RelocateField(BinaryObject &BO, uint32_t Offset, |
| 724 int64_t Value, unsigned Size) { | 727 int64_t Value, unsigned Size, |
| 728 const MachineRelocation &MR) { |
| 725 if (Size == 32) | 729 if (Size == 32) |
| 726 BO.fixWord32(Value, Offset); | 730 BO.fixWord32(Value, Offset); |
| 727 else if (Size == 64) | 731 else if (Size == 64) |
| 728 BO.fixWord64(Value, Offset); | 732 BO.fixWord64(Value, Offset); |
| 729 else | 733 else { |
| 730 llvm_unreachable("don't know howto patch relocatable field"); | 734 TERH->RelocateField(BO, Offset, Value, Size, MR); |
| 735 } |
| 731 } | 736 } |
| 732 | 737 |
| 733 /// EmitRelocations - Emit relocations | 738 /// EmitRelocations - Emit relocations |
| 734 void ELFWriter::EmitRelocations() { | 739 void ELFWriter::EmitRelocations() { |
| 735 | 740 |
| 736 // True if the target uses the relocation entry to hold the addend, | 741 // True if the target uses the relocation entry to hold the addend, |
| 737 // otherwise the addend is written directly to the relocatable field. | 742 // otherwise the addend is written directly to the relocatable field. |
| 738 bool HasRelA = TEW->hasRelocationAddend(); | 743 bool HasRelA = TEW->hasRelocationAddend(); |
| 739 | 744 |
| 740 // Create Relocation sections for each section which needs it. | 745 // Create Relocation sections for each section which needs it. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 SymIdx = SectionList[SectionIdx]->getSymbolTableIndex(); | 799 SymIdx = SectionList[SectionIdx]->getSymbolTableIndex(); |
| 795 | 800 |
| 796 // The symbol offset inside the section | 801 // The symbol offset inside the section |
| 797 int64_t SymOffset = (int64_t)MR.getResultPointer(); | 802 int64_t SymOffset = (int64_t)MR.getResultPointer(); |
| 798 | 803 |
| 799 // For pc relative relocations where symbols are defined in the same | 804 // For pc relative relocations where symbols are defined in the same |
| 800 // section they are referenced, ignore the relocation entry and patch | 805 // section they are referenced, ignore the relocation entry and patch |
| 801 // the relocatable field with the symbol offset directly. | 806 // the relocatable field with the symbol offset directly. |
| 802 if (S.SectionIdx == SectionIdx && TEW->isPCRelativeRel(RelType)) { | 807 if (S.SectionIdx == SectionIdx && TEW->isPCRelativeRel(RelType)) { |
| 803 int64_t Value = TEW->computeRelocation(SymOffset, RelOffset, RelType); | 808 int64_t Value = TEW->computeRelocation(SymOffset, RelOffset, RelType); |
| 804 RelocateField(S, RelOffset, Value, RelTySize); | 809 RelocateField(S, RelOffset, Value, RelTySize, MR); |
| 805 continue; | 810 continue; |
| 806 } | 811 } |
| 807 | 812 |
| 808 Addend = TEW->getDefaultAddendForRelTy(RelType, SymOffset); | 813 Addend = TEW->getDefaultAddendForRelTy(RelType, SymOffset); |
| 809 } | 814 } |
| 810 | 815 |
| 811 // The target without addend on the relocation symbol must be | 816 // The target without addend on the relocation symbol must be |
| 812 // patched in the relocation place itself to contain the addend | 817 // patched in the relocation place itself to contain the addend |
| 813 // otherwise write zeros to make sure there is no garbage there | 818 // otherwise write zeros to make sure there is no garbage there |
| 814 RelocateField(S, RelOffset, HasRelA ? 0 : Addend, RelTySize); | 819 RelocateField(S, RelOffset, HasRelA ? 0 : Addend, RelTySize, MR); |
| 815 | 820 |
| 816 // Get the relocation entry and emit to the relocation section | 821 // Get the relocation entry and emit to the relocation section |
| 817 ELFRelocation Rel(RelOffset, SymIdx, RelType, HasRelA, Addend); | 822 ELFRelocation Rel(RelOffset, SymIdx, RelType, HasRelA, Addend); |
| 818 EmitRelocation(RelSec, Rel, HasRelA); | 823 EmitRelocation(RelSec, Rel, HasRelA); |
| 819 } | 824 } |
| 820 } | 825 } |
| 821 } | 826 } |
| 822 | 827 |
| 823 /// EmitRelocation - Write relocation 'Rel' to the relocation section 'Rel' | 828 /// EmitRelocation - Write relocation 'Rel' to the relocation section 'Rel' |
| 824 void ELFWriter::EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, | 829 void ELFWriter::EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1081 } | 1086 } |
| 1082 | 1087 |
| 1083 // Align output for the section table. | 1088 // Align output for the section table. |
| 1084 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); | 1089 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 1085 FileOff != NewFileOff; ++FileOff) | 1090 FileOff != NewFileOff; ++FileOff) |
| 1086 O << (char)0xAB; | 1091 O << (char)0xAB; |
| 1087 | 1092 |
| 1088 // Emit the section table itself. | 1093 // Emit the section table itself. |
| 1089 O.write((char *)&SHdrTable.getData()[0], SHdrTable.size()); | 1094 O.write((char *)&SHdrTable.getData()[0], SHdrTable.size()); |
| 1090 } | 1095 } |
| OLD | NEW |