OLD | NEW |
---|---|
1 //===- subzero/src/IceELFSection.cpp - Representation of ELF sections -----===// | 1 //===- subzero/src/IceELFSection.cpp - Representation of ELF sections -----===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
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 defines how ELF sections are represented. | 10 // This file defines how ELF sections are represented. |
11 // | 11 // |
12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
13 | 13 |
14 #include "llvm/Support/MathExtras.h" | |
15 | |
14 #include "IceDefs.h" | 16 #include "IceDefs.h" |
15 #include "IceELFSection.h" | 17 #include "IceELFSection.h" |
16 #include "IceELFStreamer.h" | 18 #include "IceELFStreamer.h" |
17 | 19 |
18 using namespace llvm::ELF; | 20 using namespace llvm::ELF; |
19 | 21 |
20 namespace Ice { | 22 namespace Ice { |
21 | 23 |
22 // Text sections. | 24 // Text sections. |
23 | 25 |
24 void ELFTextSection::appendData(ELFStreamer &Str, | 26 void ELFTextSection::appendData(ELFStreamer &Str, |
25 const llvm::StringRef MoreData) { | 27 const llvm::StringRef MoreData) { |
26 Str.writeBytes(MoreData); | 28 Str.writeBytes(MoreData); |
27 Header.sh_size += MoreData.size(); | 29 Header.sh_size += MoreData.size(); |
28 } | 30 } |
29 | 31 |
30 // Data sections. | 32 // Data sections. |
31 | 33 |
32 void ELFDataSection::appendData(ELFStreamer &Str, | 34 void ELFDataSection::appendData(ELFStreamer &Str, |
33 const llvm::StringRef MoreData) { | 35 const llvm::StringRef MoreData) { |
34 Str.writeBytes(MoreData); | 36 Str.writeBytes(MoreData); |
35 Header.sh_size += MoreData.size(); | 37 Header.sh_size += MoreData.size(); |
36 } | 38 } |
37 | 39 |
40 void ELFDataSection::appendZeros(ELFStreamer &Str, SizeT NumBytes) { | |
41 Str.writeZeroPadding(NumBytes); | |
42 Header.sh_size += NumBytes; | |
43 } | |
44 | |
45 void ELFDataSection::appendRelocationOffset(ELFStreamer &Str, bool IsRela, | |
46 RelocOffsetT RelocOffset) { | |
47 if (IsRela) { | |
48 appendZeros(Str, RelocAddrSize); | |
49 return; | |
50 } | |
51 static_assert(RelocAddrSize == 4, " writeLE32 assumes RelocAddrSize is 4"); | |
52 Str.writeLE32(RelocOffset); | |
53 Header.sh_size += RelocAddrSize; | |
54 } | |
55 | |
56 void ELFDataSection::padToAlignment(ELFStreamer &Str, Elf64_Xword Align) { | |
57 assert(llvm::isPowerOf2_32(Align)); | |
58 Elf64_Xword Mod = Header.sh_size & (Align - 1); | |
59 if (Mod == 0) | |
60 return; | |
61 Elf64_Xword AlignDiff = Align - Mod; | |
Jim Stichnoth
2015/01/27 16:44:55
I've seen this pattern (which I think takes a bit
jvoung (off chromium)
2015/01/28 17:46:22
How about having something like OffsetToAlignment(
| |
62 if (Header.sh_type != llvm::ELF::SHT_NOBITS) | |
63 Str.writeZeroPadding(AlignDiff); | |
64 Header.sh_size += AlignDiff; | |
65 assert((Header.sh_size & (Align - 1)) == 0); | |
66 } | |
67 | |
38 // Relocation sections. | 68 // Relocation sections. |
39 | 69 |
40 void ELFRelocationSection::addRelocations(RelocOffsetT BaseOff, | 70 void ELFRelocationSection::addRelocations(RelocOffsetT BaseOff, |
41 const FixupRefList &FixupRefs) { | 71 const FixupRefList &FixupRefs) { |
42 for (const AssemblerFixup *FR : FixupRefs) { | 72 for (const AssemblerFixup *FR : FixupRefs) { |
43 Fixups.push_back(*FR); | 73 Fixups.push_back(*FR); |
44 AssemblerFixup &F = Fixups.back(); | 74 AssemblerFixup &F = Fixups.back(); |
45 F.set_position(BaseOff + F.position()); | 75 F.set_position(BaseOff + F.position()); |
46 } | 76 } |
47 } | 77 } |
(...skipping 18 matching lines...) Expand all Loading... | |
66 uint8_t Type, uint8_t Binding, | 96 uint8_t Type, uint8_t Binding, |
67 ELFSection *Section, | 97 ELFSection *Section, |
68 RelocOffsetT Offset, SizeT Size) { | 98 RelocOffsetT Offset, SizeT Size) { |
69 ELFSym NewSymbol = ELFSym(); | 99 ELFSym NewSymbol = ELFSym(); |
70 NewSymbol.Sym.setBindingAndType(Binding, Type); | 100 NewSymbol.Sym.setBindingAndType(Binding, Type); |
71 NewSymbol.Sym.st_value = Offset; | 101 NewSymbol.Sym.st_value = Offset; |
72 NewSymbol.Sym.st_size = Size; | 102 NewSymbol.Sym.st_size = Size; |
73 NewSymbol.Section = Section; | 103 NewSymbol.Section = Section; |
74 NewSymbol.Number = ELFSym::UnknownNumber; | 104 NewSymbol.Number = ELFSym::UnknownNumber; |
75 bool Unique; | 105 bool Unique; |
76 if (Type == STB_LOCAL) | 106 if (Binding == STB_LOCAL) |
77 Unique = LocalSymbols.insert(std::make_pair(Name, NewSymbol)).second; | 107 Unique = LocalSymbols.insert(std::make_pair(Name, NewSymbol)).second; |
78 else | 108 else |
79 Unique = GlobalSymbols.insert(std::make_pair(Name, NewSymbol)).second; | 109 Unique = GlobalSymbols.insert(std::make_pair(Name, NewSymbol)).second; |
80 assert(Unique); | 110 assert(Unique); |
81 (void)Unique; | 111 (void)Unique; |
82 } | 112 } |
83 | 113 |
84 void ELFSymbolTableSection::noteUndefinedSym(const IceString &Name, | 114 void ELFSymbolTableSection::noteUndefinedSym(const IceString &Name, |
85 ELFSection *NullSection) { | 115 ELFSection *NullSection) { |
86 ELFSym NewSymbol = ELFSym(); | 116 ELFSym NewSymbol = ELFSym(); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 continue; | 214 continue; |
185 } | 215 } |
186 StringIndex.second = StringData.size(); | 216 StringIndex.second = StringData.size(); |
187 std::copy(Cur.begin(), Cur.end(), back_inserter(StringData)); | 217 std::copy(Cur.begin(), Cur.end(), back_inserter(StringData)); |
188 StringData.push_back(0); | 218 StringData.push_back(0); |
189 Prev = Cur; | 219 Prev = Cur; |
190 } | 220 } |
191 } | 221 } |
192 | 222 |
193 } // end of namespace Ice | 223 } // end of namespace Ice |
OLD | NEW |