annotate tools/llvm-objcopy/Object.cpp @ 122:36195a0db682

merging ( incomplete )
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 17 Nov 2017 20:32:31 +0900
parents 803732b1fca8
children 3a76565eade5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===- Object.cpp -----------------------------------------------*- C++ -*-===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 #include "Object.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 #include "llvm-objcopy.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 using namespace object;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 using namespace ELF;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 template <class ELFT> void Segment::writeHeader(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 typedef typename ELFT::Ehdr Elf_Ehdr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 typedef typename ELFT::Phdr Elf_Phdr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 uint8_t *Buf = Out.getBufferStart();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 Buf += sizeof(Elf_Ehdr) + Index * sizeof(Elf_Phdr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(Buf);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 Phdr.p_type = Type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 Phdr.p_flags = Flags;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 Phdr.p_offset = Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 Phdr.p_vaddr = VAddr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 Phdr.p_paddr = PAddr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 Phdr.p_filesz = FileSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 Phdr.p_memsz = MemSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 Phdr.p_align = Align;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 void Segment::writeSegment(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 uint8_t *Buf = Out.getBufferStart() + Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 // We want to maintain segments' interstitial data and contents exactly.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 // This lets us just copy segments directly.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 std::copy(std::begin(Contents), std::end(Contents), Buf);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 void SectionBase::initialize(SectionTableRef SecTable) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 void SectionBase::finalize() {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 void SectionBase::writeHeader(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 uint8_t *Buf = Out.getBufferStart();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 Buf += HeaderOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(Buf);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 Shdr.sh_name = NameIndex;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 Shdr.sh_type = Type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 Shdr.sh_flags = Flags;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 Shdr.sh_addr = Addr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 Shdr.sh_offset = Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 Shdr.sh_size = Size;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 Shdr.sh_link = Link;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 Shdr.sh_info = Info;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 Shdr.sh_addralign = Align;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 Shdr.sh_entsize = EntrySize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 void Section::writeSection(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 if (Type == SHT_NOBITS)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 uint8_t *Buf = Out.getBufferStart() + Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 std::copy(std::begin(Contents), std::end(Contents), Buf);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 void StringTableSection::addString(StringRef Name) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 StrTabBuilder.add(Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 Size = StrTabBuilder.getSize();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 uint32_t StringTableSection::findIndex(StringRef Name) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 return StrTabBuilder.getOffset(Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 void StringTableSection::finalize() { StrTabBuilder.finalize(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 void StringTableSection::writeSection(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 StrTabBuilder.write(Out.getBufferStart() + Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 switch (Index) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 case SHN_ABS:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 case SHN_COMMON:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 return true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 if (Machine == EM_HEXAGON) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 switch (Index) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 case SHN_HEXAGON_SCOMMON:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 case SHN_HEXAGON_SCOMMON_2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 case SHN_HEXAGON_SCOMMON_4:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 case SHN_HEXAGON_SCOMMON_8:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 return true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 uint16_t Symbol::getShndx() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 if (DefinedIn != nullptr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 return DefinedIn->Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 switch (ShndxType) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 // This means that we don't have a defined section but we do need to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 // output a legitimate section index.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 case SYMBOL_SIMPLE_INDEX:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 return SHN_UNDEF;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 case SYMBOL_ABS:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 case SYMBOL_COMMON:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 case SYMBOL_HEXAGON_SCOMMON:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 case SYMBOL_HEXAGON_SCOMMON_2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 case SYMBOL_HEXAGON_SCOMMON_4:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 case SYMBOL_HEXAGON_SCOMMON_8:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 return static_cast<uint16_t>(ShndxType);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 llvm_unreachable("Symbol with invalid ShndxType encountered");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 SectionBase *DefinedIn, uint64_t Value,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123 uint16_t Shndx, uint64_t Sz) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 Symbol Sym;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 Sym.Name = Name;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 Sym.Binding = Bind;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 Sym.Type = Type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 Sym.DefinedIn = DefinedIn;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 if (DefinedIn == nullptr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 if (Shndx >= SHN_LORESERVE)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 Sym.Value = Value;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 Sym.Size = Sz;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 Sym.Index = Symbols.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 Size += this->EntrySize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 if (SymbolNames == Sec) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 error("String table " + SymbolNames->Name +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 " cannot be removed because it is referenced by the symbol table " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 this->Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 auto Iter =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 std::remove_if(std::begin(Symbols), std::end(Symbols),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 [=](const SymPtr &Sym) { return Sym->DefinedIn == Sec; });
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 Size -= (std::end(Symbols) - Iter) * this->EntrySize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 Symbols.erase(Iter, std::end(Symbols));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 void SymbolTableSection::initialize(SectionTableRef SecTable) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 Size = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 setStrTab(SecTable.getSectionOfType<StringTableSection>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 Link,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 "Symbol table has link index of " + Twine(Link) +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 " which is not a valid index",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 "Symbol table has link index of " + Twine(Link) +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 " which is not a string table"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 void SymbolTableSection::finalize() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 // Make sure SymbolNames is finalized before getting name indexes.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 SymbolNames->finalize();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 uint32_t MaxLocalIndex = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 for (auto &Sym : Symbols) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172 if (Sym->Binding == STB_LOCAL)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 // Now we need to set the Link and Info fields.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 Link = SymbolNames->Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177 Info = MaxLocalIndex + 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 void SymbolTableSection::addSymbolNames() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 // Add all of our strings to SymbolNames so that SymbolNames has the right
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 // size before layout is decided.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183 for (auto &Sym : Symbols)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 SymbolNames->addString(Sym->Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188 if (Symbols.size() <= Index)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 error("Invalid symbol index: " + Twine(Index));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 return Symbols[Index].get();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194 void SymbolTableSectionImpl<ELFT>::writeSection(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 llvm::FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 uint8_t *Buf = Out.getBufferStart();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 Buf += Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 // Loop though symbols setting each entry of the symbol table.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200 for (auto &Symbol : Symbols) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 Sym->st_name = Symbol->NameIndex;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 Sym->st_value = Symbol->Value;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 Sym->st_size = Symbol->Size;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 Sym->setBinding(Symbol->Binding);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 Sym->setType(Symbol->Type);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 Sym->st_shndx = Symbol->getShndx();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207 ++Sym;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211 template <class SymTabType>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 const SectionBase *Sec) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214 if (Symbols == Sec) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 error("Symbol table " + Symbols->Name + " cannot be removed because it is "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 "referenced by the relocation "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 "section " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218 this->Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222 template <class SymTabType>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223 void RelocSectionWithSymtabBase<SymTabType>::initialize(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224 SectionTableRef SecTable) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225 setSymTab(SecTable.getSectionOfType<SymTabType>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226 Link,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227 "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228 "Link field value " + Twine(Link) + " in section " + Name +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229 " is not a symbol table"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231 if (Info != SHN_UNDEF)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 setSection(SecTable.getSection(Info,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 "Info field value " + Twine(Info) +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234 " in section " + Name + " is invalid"));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236 setSection(nullptr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239 template <class SymTabType>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 void RelocSectionWithSymtabBase<SymTabType>::finalize() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241 this->Link = Symbols->Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242 if (SecToApplyRel != nullptr)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243 this->Info = SecToApplyRel->Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247 void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250 void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251 Rela.r_addend = Addend;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 template <class T>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256 void RelocationSection<ELFT>::writeRel(T *Buf) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257 for (const auto &Reloc : Relocations) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258 Buf->r_offset = Reloc.Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259 setAddend(*Buf, Reloc.Addend);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261 ++Buf;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266 void RelocationSection<ELFT>::writeSection(llvm::FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
267 uint8_t *Buf = Out.getBufferStart() + Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
268 if (Type == SHT_REL)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
269 writeRel(reinterpret_cast<Elf_Rel *>(Buf));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
270 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
271 writeRel(reinterpret_cast<Elf_Rela *>(Buf));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
272 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
273
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
274 void DynamicRelocationSection::writeSection(llvm::FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
275 std::copy(std::begin(Contents), std::end(Contents),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
276 Out.getBufferStart() + Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
277 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
278
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
279 void SectionWithStrTab::removeSectionReferences(const SectionBase *Sec) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
280 if (StrTab == Sec) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
281 error("String table " + StrTab->Name + " cannot be removed because it is "
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
282 "referenced by the section " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
283 this->Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
284 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
285 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
286
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
287 bool SectionWithStrTab::classof(const SectionBase *S) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
288 return isa<DynamicSymbolTableSection>(S) || isa<DynamicSection>(S);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
289 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
290
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
291 void SectionWithStrTab::initialize(SectionTableRef SecTable) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
292 auto StrTab = SecTable.getSection(Link,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
293 "Link field value " + Twine(Link) +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
294 " in section " + Name + " is invalid");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
295 if (StrTab->Type != SHT_STRTAB) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
296 error("Link field value " + Twine(Link) + " in section " + Name +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
297 " is not a string table");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
298 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
299 setStrTab(StrTab);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
300 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
301
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
302 void SectionWithStrTab::finalize() { this->Link = StrTab->Index; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
303
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
304 // Returns true IFF a section is wholly inside the range of a segment
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
305 static bool sectionWithinSegment(const SectionBase &Section,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
306 const Segment &Segment) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
307 // If a section is empty it should be treated like it has a size of 1. This is
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
308 // to clarify the case when an empty section lies on a boundary between two
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
309 // segments and ensures that the section "belongs" to the second segment and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
310 // not the first.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
311 uint64_t SecSize = Section.Size ? Section.Size : 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
312 return Segment.Offset <= Section.OriginalOffset &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
313 Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
314 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
315
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
316 // Returns true IFF a segment's original offset is inside of another segment's
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317 // range.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318 static bool segmentOverlapsSegment(const Segment &Child,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 const Segment &Parent) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321 return Parent.OriginalOffset <= Child.OriginalOffset &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322 Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326 void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327 uint32_t Index = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328 for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329 ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 (size_t)Phdr.p_filesz};
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331 Segments.emplace_back(llvm::make_unique<Segment>(Data));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332 Segment &Seg = *Segments.back();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 Seg.Type = Phdr.p_type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334 Seg.Flags = Phdr.p_flags;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335 Seg.OriginalOffset = Phdr.p_offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
336 Seg.Offset = Phdr.p_offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
337 Seg.VAddr = Phdr.p_vaddr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
338 Seg.PAddr = Phdr.p_paddr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
339 Seg.FileSize = Phdr.p_filesz;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
340 Seg.MemSize = Phdr.p_memsz;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
341 Seg.Align = Phdr.p_align;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
342 Seg.Index = Index++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
343 for (auto &Section : Sections) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
344 if (sectionWithinSegment(*Section, Seg)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
345 Seg.addSection(&*Section);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
346 if (!Section->ParentSegment ||
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
347 Section->ParentSegment->Offset > Seg.Offset) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
348 Section->ParentSegment = &Seg;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
349 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
350 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
351 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
352 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
353 // Now we do an O(n^2) loop through the segments in order to match up
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
354 // segments.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
355 for (auto &Child : Segments) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
356 for (auto &Parent : Segments) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
357 // Every segment will overlap with itself but we don't want a segment to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
358 // be it's own parent so we avoid that situation.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
359 if (&Child != &Parent && segmentOverlapsSegment(*Child, *Parent)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
360 // We want a canonical "most parental" segment but this requires
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
361 // inspecting the ParentSegment.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
362 if (Child->ParentSegment != nullptr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
363 if (Child->ParentSegment->OriginalOffset > Parent->OriginalOffset) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
364 Child->ParentSegment = Parent.get();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
365 } else if (Child->ParentSegment->Index > Parent->Index) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
366 // They must have equal OriginalOffsets so we need to disambiguate.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
367 // To decide which is the parent we'll choose the one with the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
368 // higher index.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
369 Child->ParentSegment = Parent.get();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
370 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
371 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
372 Child->ParentSegment = Parent.get();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
373 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
374 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
375 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
376 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
377 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
378
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
379 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
380 void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
381 SymbolTableSection *SymTab,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
382 SectionTableRef SecTable) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
383
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
384 const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
385 StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
386
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
387 for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
388 SectionBase *DefSection = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
389 StringRef Name = unwrapOrError(Sym.getName(StrTabData));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
390
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
391 if (Sym.st_shndx >= SHN_LORESERVE) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
392 if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
393 error(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
394 "Symbol '" + Name +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
395 "' has unsupported value greater than or equal to SHN_LORESERVE: " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
396 Twine(Sym.st_shndx));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
397 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
398 } else if (Sym.st_shndx != SHN_UNDEF) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
399 DefSection = SecTable.getSection(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
400 Sym.st_shndx,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
401 "Symbol '" + Name + "' is defined in invalid section with index " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
402 Twine(Sym.st_shndx));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
403 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
404
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
405 SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
406 Sym.getValue(), Sym.st_shndx, Sym.st_size);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
407 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
408 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
409
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
410 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
411 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
412
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
413 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
414 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
415 ToSet = Rela.r_addend;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
416 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
417
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
418 template <class ELFT, class T>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
419 void initRelocations(RelocationSection<ELFT> *Relocs,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
420 SymbolTableSection *SymbolTable, T RelRange) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
421 for (const auto &Rel : RelRange) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
422 Relocation ToAdd;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
423 ToAdd.Offset = Rel.r_offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
424 getAddend(ToAdd.Addend, Rel);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
425 ToAdd.Type = Rel.getType(false);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
426 ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
427 Relocs->addRelocation(ToAdd);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
428 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
429 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
430
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
431 SectionBase *SectionTableRef::getSection(uint16_t Index, Twine ErrMsg) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
432 if (Index == SHN_UNDEF || Index > Sections.size())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
433 error(ErrMsg);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
434 return Sections[Index - 1].get();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
435 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
436
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
437 template <class T>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
438 T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
439 Twine TypeErrMsg) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
440 if (T *Sec = llvm::dyn_cast<T>(getSection(Index, IndexErrMsg)))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
441 return Sec;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
442 error(TypeErrMsg);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
443 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
444
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
445 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
446 std::unique_ptr<SectionBase>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
447 Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
448 const Elf_Shdr &Shdr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
449 ArrayRef<uint8_t> Data;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
450 switch (Shdr.sh_type) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
451 case SHT_REL:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
452 case SHT_RELA:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
453 if (Shdr.sh_flags & SHF_ALLOC) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
454 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
455 return llvm::make_unique<DynamicRelocationSection>(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
456 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
457 return llvm::make_unique<RelocationSection<ELFT>>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
458 case SHT_STRTAB:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
459 // If a string table is allocated we don't want to mess with it. That would
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
460 // mean altering the memory image. There are no special link types or
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
461 // anything so we can just use a Section.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
462 if (Shdr.sh_flags & SHF_ALLOC) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
463 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
464 return llvm::make_unique<Section>(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
465 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
466 return llvm::make_unique<StringTableSection>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
467 case SHT_HASH:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
468 case SHT_GNU_HASH:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
469 // Hash tables should refer to SHT_DYNSYM which we're not going to change.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
470 // Because of this we don't need to mess with the hash tables either.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
471 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
472 return llvm::make_unique<Section>(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
473 case SHT_DYNSYM:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
474 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
475 return llvm::make_unique<DynamicSymbolTableSection>(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
476 case SHT_DYNAMIC:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
477 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
478 return llvm::make_unique<DynamicSection>(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
479 case SHT_SYMTAB: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
480 auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
481 SymbolTable = SymTab.get();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
482 return std::move(SymTab);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
483 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
484 case SHT_NOBITS:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
485 return llvm::make_unique<Section>(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
486 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
487 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
488 return llvm::make_unique<Section>(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
489 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
490 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
491
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
492 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
493 SectionTableRef Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
494 uint32_t Index = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
495 for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
496 if (Index == 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
497 ++Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
498 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
499 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
500 SecPtr Sec = makeSection(ElfFile, Shdr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
501 Sec->Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
502 Sec->Type = Shdr.sh_type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
503 Sec->Flags = Shdr.sh_flags;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
504 Sec->Addr = Shdr.sh_addr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
505 Sec->Offset = Shdr.sh_offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
506 Sec->OriginalOffset = Shdr.sh_offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
507 Sec->Size = Shdr.sh_size;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
508 Sec->Link = Shdr.sh_link;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
509 Sec->Info = Shdr.sh_info;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
510 Sec->Align = Shdr.sh_addralign;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
511 Sec->EntrySize = Shdr.sh_entsize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
512 Sec->Index = Index++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
513 Sections.push_back(std::move(Sec));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
514 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
515
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
516 SectionTableRef SecTable(Sections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
517
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
518 // Now that all of the sections have been added we can fill out some extra
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
519 // details about symbol tables. We need the symbol table filled out before
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
520 // any relocations.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
521 if (SymbolTable) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
522 SymbolTable->initialize(SecTable);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
523 initSymbolTable(ElfFile, SymbolTable, SecTable);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
524 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
525
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
526 // Now that all sections and symbols have been added we can add
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
527 // relocations that reference symbols and set the link and info fields for
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
528 // relocation sections.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
529 for (auto &Section : Sections) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
530 if (Section.get() == SymbolTable)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
531 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
532 Section->initialize(SecTable);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
533 if (auto RelSec = dyn_cast<RelocationSection<ELFT>>(Section.get())) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
534 auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
535 if (RelSec->Type == SHT_REL)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
536 initRelocations(RelSec, SymbolTable, unwrapOrError(ElfFile.rels(Shdr)));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
537 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
538 initRelocations(RelSec, SymbolTable,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
539 unwrapOrError(ElfFile.relas(Shdr)));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
540 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
541 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
542
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
543 return SecTable;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
544 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
545
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
546 template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
547 const auto &ElfFile = *Obj.getELFFile();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
548 const auto &Ehdr = *ElfFile.getHeader();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
549
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
550 std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Ident);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
551 Type = Ehdr.e_type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
552 Machine = Ehdr.e_machine;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
553 Version = Ehdr.e_version;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
554 Entry = Ehdr.e_entry;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
555 Flags = Ehdr.e_flags;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
556
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
557 SectionTableRef SecTable = readSectionHeaders(ElfFile);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
558 readProgramHeaders(ElfFile);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
559
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
560 SectionNames = SecTable.getSectionOfType<StringTableSection>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
561 Ehdr.e_shstrndx,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
562 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
563 " is invalid",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
564 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
565 " is not a string table");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
566 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
567
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
568 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
569 void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
570 uint8_t *Buf = Out.getBufferStart();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
571 Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(Buf);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
572 std::copy(Ident, Ident + 16, Ehdr.e_ident);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
573 Ehdr.e_type = Type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
574 Ehdr.e_machine = Machine;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
575 Ehdr.e_version = Version;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
576 Ehdr.e_entry = Entry;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
577 Ehdr.e_phoff = sizeof(Elf_Ehdr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
578 Ehdr.e_flags = Flags;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
579 Ehdr.e_ehsize = sizeof(Elf_Ehdr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
580 Ehdr.e_phentsize = sizeof(Elf_Phdr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
581 Ehdr.e_phnum = Segments.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
582 Ehdr.e_shentsize = sizeof(Elf_Shdr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
583 if (WriteSectionHeaders) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
584 Ehdr.e_shoff = SHOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
585 Ehdr.e_shnum = Sections.size() + 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
586 Ehdr.e_shstrndx = SectionNames->Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
587 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
588 Ehdr.e_shoff = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
589 Ehdr.e_shnum = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
590 Ehdr.e_shstrndx = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
591 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
592 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
593
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
594 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
595 void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
596 for (auto &Phdr : Segments)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
597 Phdr->template writeHeader<ELFT>(Out);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
598 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
599
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
600 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
601 void Object<ELFT>::writeSectionHeaders(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
602 uint8_t *Buf = Out.getBufferStart() + SHOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
603 // This reference serves to write the dummy section header at the begining
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
604 // of the file. It is not used for anything else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
605 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
606 Shdr.sh_name = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
607 Shdr.sh_type = SHT_NULL;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
608 Shdr.sh_flags = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
609 Shdr.sh_addr = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
610 Shdr.sh_offset = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
611 Shdr.sh_size = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
612 Shdr.sh_link = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
613 Shdr.sh_info = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
614 Shdr.sh_addralign = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
615 Shdr.sh_entsize = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
616
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
617 for (auto &Section : Sections)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
618 Section->template writeHeader<ELFT>(Out);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
619 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
620
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
621 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
622 void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
623 for (auto &Section : Sections)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
624 Section->writeSection(Out);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
625 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
626
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
627 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
628 void Object<ELFT>::removeSections(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
629 std::function<bool(const SectionBase &)> ToRemove) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
630
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
631 auto Iter = std::stable_partition(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
632 std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
633 if (ToRemove(*Sec))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
634 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
635 if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
636 if (auto ToRelSec = RelSec->getSection())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
637 return !ToRemove(*ToRelSec);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
638 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
639 return true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
640 });
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
641 if (SymbolTable != nullptr && ToRemove(*SymbolTable))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
642 SymbolTable = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
643 if (ToRemove(*SectionNames)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
644 if (WriteSectionHeaders)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
645 error("Cannot remove " + SectionNames->Name +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
646 " because it is the section header string table.");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
647 SectionNames = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
648 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
649 // Now make sure there are no remaining references to the sections that will
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
650 // be removed. Sometimes it is impossible to remove a reference so we emit
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
651 // an error here instead.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
652 for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
653 for (auto &Segment : Segments)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
654 Segment->removeSection(RemoveSec.get());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
655 for (auto &KeepSec : make_range(std::begin(Sections), Iter))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
656 KeepSec->removeSectionReferences(RemoveSec.get());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
657 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
658 // Now finally get rid of them all togethor.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
659 Sections.erase(Iter, std::end(Sections));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
660 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
661
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
662 template <class ELFT> void ELFObject<ELFT>::sortSections() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
663 // Put all sections in offset order. Maintain the ordering as closely as
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
664 // possible while meeting that demand however.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
665 auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
666 return A->OriginalOffset < B->OriginalOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
667 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
668 std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
669 CompareSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
670 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
671
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
672 template <class ELFT> void ELFObject<ELFT>::assignOffsets() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
673 // We need a temporary list of segments that has a special order to it
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
674 // so that we know that anytime ->ParentSegment is set that segment has
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
675 // already had it's offset properly set.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
676 std::vector<Segment *> OrderedSegments;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
677 for (auto &Segment : this->Segments)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
678 OrderedSegments.push_back(Segment.get());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
679 auto CompareSegments = [](const Segment *A, const Segment *B) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
680 // Any segment without a parent segment should come before a segment
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
681 // that has a parent segment.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
682 if (A->OriginalOffset < B->OriginalOffset)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
683 return true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
684 if (A->OriginalOffset > B->OriginalOffset)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
685 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
686 return A->Index < B->Index;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
687 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
688 std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
689 CompareSegments);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
690 // The size of ELF + program headers will not change so it is ok to assume
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
691 // that the first offset of the first segment is a good place to start
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
692 // outputting sections. This covers both the standard case and the PT_PHDR
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
693 // case.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
694 uint64_t Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
695 if (!OrderedSegments.empty()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
696 Offset = OrderedSegments[0]->Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
697 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
698 Offset = sizeof(Elf_Ehdr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
699 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
700 // The only way a segment should move is if a section was between two
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
701 // segments and that section was removed. If that section isn't in a segment
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
702 // then it's acceptable, but not ideal, to simply move it to after the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
703 // segments. So we can simply layout segments one after the other accounting
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
704 // for alignment.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
705 for (auto &Segment : OrderedSegments) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
706 // We assume that segments have been ordered by OriginalOffset and Index
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
707 // such that a parent segment will always come before a child segment in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
708 // OrderedSegments. This means that the Offset of the ParentSegment should
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
709 // already be set and we can set our offset relative to it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
710 if (Segment->ParentSegment != nullptr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
711 auto Parent = Segment->ParentSegment;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
712 Segment->Offset =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
713 Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
714 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
715 Offset = alignTo(Offset, Segment->Align == 0 ? 1 : Segment->Align);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
716 Segment->Offset = Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
717 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
718 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
719 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
720 // Now the offset of every segment has been set we can assign the offsets
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
721 // of each section. For sections that are covered by a segment we should use
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
722 // the segment's original offset and the section's original offset to compute
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
723 // the offset from the start of the segment. Using the offset from the start
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
724 // of the segment we can assign a new offset to the section. For sections not
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
725 // covered by segments we can just bump Offset to the next valid location.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
726 uint32_t Index = 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
727 for (auto &Section : this->Sections) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
728 Section->Index = Index++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
729 if (Section->ParentSegment != nullptr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
730 auto Segment = Section->ParentSegment;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
731 Section->Offset =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
732 Segment->Offset + (Section->OriginalOffset - Segment->OriginalOffset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
733 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
734 Offset = alignTo(Offset, Section->Align == 0 ? 1 : Section->Align);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
735 Section->Offset = Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
736 if (Section->Type != SHT_NOBITS)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
737 Offset += Section->Size;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
738 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
739 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
740
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
741 if (this->WriteSectionHeaders) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
742 Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
743 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
744 this->SHOffset = Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
745 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
746
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
747 template <class ELFT> size_t ELFObject<ELFT>::totalSize() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
748 // We already have the section header offset so we can calculate the total
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
749 // size by just adding up the size of each section header.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
750 auto NullSectionSize = this->WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
751 return this->SHOffset + this->Sections.size() * sizeof(Elf_Shdr) +
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
752 NullSectionSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
753 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
754
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
755 template <class ELFT> void ELFObject<ELFT>::write(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
756 this->writeHeader(Out);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
757 this->writeProgramHeaders(Out);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
758 this->writeSectionData(Out);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
759 if (this->WriteSectionHeaders)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
760 this->writeSectionHeaders(Out);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
761 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
762
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
763 template <class ELFT> void ELFObject<ELFT>::finalize() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
764 // Make sure we add the names of all the sections.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
765 if (this->SectionNames != nullptr)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
766 for (const auto &Section : this->Sections) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
767 this->SectionNames->addString(Section->Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
768 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
769 // Make sure we add the names of all the symbols.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
770 if (this->SymbolTable != nullptr)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
771 this->SymbolTable->addSymbolNames();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
772
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
773 sortSections();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
774 assignOffsets();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
775
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
776 // Finalize SectionNames first so that we can assign name indexes.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
777 if (this->SectionNames != nullptr)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
778 this->SectionNames->finalize();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
779 // Finally now that all offsets and indexes have been set we can finalize any
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
780 // remaining issues.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
781 uint64_t Offset = this->SHOffset + sizeof(Elf_Shdr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
782 for (auto &Section : this->Sections) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
783 Section->HeaderOffset = Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
784 Offset += sizeof(Elf_Shdr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
785 if (this->WriteSectionHeaders)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
786 Section->NameIndex = this->SectionNames->findIndex(Section->Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
787 Section->finalize();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
788 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
789 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
790
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
791 template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
792 return TotalSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
793 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
794
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
795 template <class ELFT>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
796 void BinaryObject<ELFT>::write(FileOutputBuffer &Out) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
797 for (auto &Segment : this->Segments) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
798 // GNU objcopy does not output segments that do not cover a section. Such
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
799 // segments can sometimes be produced by LLD due to how LLD handles PT_PHDR.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
800 if (Segment->Type == llvm::ELF::PT_LOAD &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
801 Segment->firstSection() != nullptr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
802 Segment->writeSegment(Out);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
803 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
804 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
805 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
806
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
807 template <class ELFT> void BinaryObject<ELFT>::finalize() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
808
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
809 // Put all segments in offset order.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
810 auto CompareSegments = [](const SegPtr &A, const SegPtr &B) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
811 return A->Offset < B->Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
812 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
813 std::sort(std::begin(this->Segments), std::end(this->Segments),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
814 CompareSegments);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
815
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
816 uint64_t Offset = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
817 for (auto &Segment : this->Segments) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
818 if (Segment->Type == llvm::ELF::PT_LOAD &&
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
819 Segment->firstSection() != nullptr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
820 Offset = alignTo(Offset, Segment->Align);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
821 Segment->Offset = Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
822 Offset += Segment->FileSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
823 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
824 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
825 TotalSize = Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
826 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
827
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
828 template class Object<ELF64LE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
829 template class Object<ELF64BE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
830 template class Object<ELF32LE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
831 template class Object<ELF32BE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
832
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
833 template class ELFObject<ELF64LE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
834 template class ELFObject<ELF64BE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
835 template class ELFObject<ELF32LE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
836 template class ELFObject<ELF32BE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
837
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
838 template class BinaryObject<ELF64LE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
839 template class BinaryObject<ELF64BE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
840 template class BinaryObject<ELF32LE>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
841 template class BinaryObject<ELF32BE>;