llvm.org GIT mirror llvm / 6ccdeef
[ObjectYAML] [COFF] Support multiple symbols with the same name Differential Revision: https://reviews.llvm.org/D56294 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350566 91177308-0d34-0410-b5e6-96231b3b80d8 Martin Storsjo 8 months ago
5 changed file(s) with 109 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
5757 struct Relocation {
5858 uint32_t VirtualAddress;
5959 uint16_t Type;
60
61 // Normally a Relocation can refer to the symbol via its name.
62 // It can also use a direct symbol table index instead (with no name
63 // specified), allowing disambiguating between multiple symbols with the
64 // same name or crafting intentionally broken files for testing.
6065 StringRef SymbolName;
66 Optional SymbolTableIndex;
6167 };
6268
6369 struct Section {
406406 void MappingTraits::mapping(IO &IO,
407407 COFFYAML::Relocation &Rel) {
408408 IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
409 IO.mapRequired("SymbolName", Rel.SymbolName);
409 IO.mapOptional("SymbolName", Rel.SymbolName, StringRef());
410 IO.mapOptional("SymbolTableIndex", Rel.SymbolTableIndex);
410411
411412 COFF::header &H = *static_cast(IO.getContext());
412413 if (H.Machine == COFF::IMAGE_FILE_MACHINE_I386) {
0 # RUN: yaml2obj %s -o %t
1 # RUN: llvm-readobj -relocations %t | FileCheck %s --check-prefix=RELOCS
2 # RUN: obj2yaml %t | FileCheck %s --check-prefix=YAML
3
4 # RELOCS: Relocations [
5 # RELOCS-NEXT: Section (1) .text {
6 # RELOCS-NEXT: 0x3 IMAGE_REL_AMD64_REL32 .rdata (0)
7 # RELOCS-NEXT: 0xA IMAGE_REL_AMD64_REL32 .rdata (1)
8 # RELOCS-NEXT: 0x11 IMAGE_REL_AMD64_REL32 foo (2)
9 # RELOCS-NEXT: }
10 # RELOCS-NEXT: ]
11
12 # Check that we usually output relocations with SymbolName.
13 # For relocations with a non-unique symbol name, output
14 # SymbolTableIndex instead.
15
16 # YAML: Relocations:
17 # YAML-NEXT: - VirtualAddress: 3
18 # YAML-NEXT: SymbolTableIndex: 0
19 # YAML-NEXT: Type: IMAGE_REL_AMD64_REL32
20 # YAML-NEXT: - VirtualAddress: 10
21 # YAML-NEXT: SymbolTableIndex: 1
22 # YAML-NEXT: Type: IMAGE_REL_AMD64_REL32
23 # YAML-NEXT: - VirtualAddress: 17
24 # YAML-NEXT: SymbolName: foo
25 # YAML-NEXT: Type: IMAGE_REL_AMD64_REL32
26
27 --- !COFF
28 header:
29 Machine: IMAGE_FILE_MACHINE_AMD64
30 Characteristics: [ ]
31 sections:
32 - Name: .text
33 Characteristics: [ ]
34 Alignment: 4
35 SectionData: 488B0500000000488B0500000000488B0500000000
36 Relocations:
37 - VirtualAddress: 3
38 SymbolTableIndex: 0
39 Type: IMAGE_REL_AMD64_REL32
40 - VirtualAddress: 10
41 SymbolTableIndex: 1
42 Type: IMAGE_REL_AMD64_REL32
43 - VirtualAddress: 17
44 SymbolName: foo
45 Type: IMAGE_REL_AMD64_REL32
46 - Name: .rdata
47 Characteristics: [ ]
48 Alignment: 1
49 SectionData: '00'
50 - Name: .rdata
51 Characteristics: [ ]
52 Alignment: 1
53 SectionData: '01'
54 symbols:
55 - Name: .rdata
56 Value: 0
57 SectionNumber: 2
58 SimpleType: IMAGE_SYM_TYPE_NULL
59 ComplexType: IMAGE_SYM_DTYPE_NULL
60 StorageClass: IMAGE_SYM_CLASS_STATIC
61 - Name: .rdata
62 Value: 0
63 SectionNumber: 3
64 SimpleType: IMAGE_SYM_TYPE_NULL
65 ComplexType: IMAGE_SYM_DTYPE_NULL
66 StorageClass: IMAGE_SYM_CLASS_STATIC
67 - Name: foo
68 Value: 0
69 SectionNumber: 3
70 SimpleType: IMAGE_SYM_TYPE_NULL
71 ComplexType: IMAGE_SYM_DTYPE_NULL
72 StorageClass: IMAGE_SYM_CLASS_EXTERNAL
73 ...
77 //===----------------------------------------------------------------------===//
88
99 #include "obj2yaml.h"
10 #include "llvm/ADT/StringMap.h"
1011 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
1112 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
1213 #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
141142 codeview::StringsAndChecksumsRef SC;
142143 initializeFileAndStringTable(Obj, SC);
143144
145 StringMap SymbolUnique;
146 for (const auto &S : Obj.symbols()) {
147 object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S);
148 StringRef Name;
149 Obj.getSymbolName(Symbol, Name);
150 StringMap::iterator It;
151 bool Inserted;
152 std::tie(It, Inserted) = SymbolUnique.insert(std::make_pair(Name, true));
153 if (!Inserted)
154 It->second = false;
155 }
156
144157 for (const auto &ObjSection : Obj.sections()) {
145158 const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection);
146159 COFFYAML::Section NewYAMLSection;
191204 OS.flush();
192205 report_fatal_error(Buf);
193206 }
194 Rel.SymbolName = *SymbolNameOrErr;
207 if (SymbolUnique.lookup(*SymbolNameOrErr))
208 Rel.SymbolName = *SymbolNameOrErr;
209 else
210 Rel.SymbolTableIndex = reloc->SymbolTableIndex;
195211 Rel.VirtualAddress = reloc->VirtualAddress;
196212 Rel.Type = reloc->Type;
197213 Relocations.push_back(Rel);
2323 #include "llvm/Support/Endian.h"
2424 #include "llvm/Support/MemoryBuffer.h"
2525 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/WithColor.h"
2627 #include "llvm/Support/raw_ostream.h"
2728 #include
2829
519520 assert(S.Header.SizeOfRawData >= S.SectionData.binary_size());
520521 OS << num_zeros(S.Header.SizeOfRawData - S.SectionData.binary_size());
521522 for (const COFFYAML::Relocation &R : S.Relocations) {
522 uint32_t SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
523 uint32_t SymbolTableIndex;
524 if (R.SymbolTableIndex) {
525 if (!R.SymbolName.empty())
526 WithColor::error()
527 << "Both SymbolName and SymbolTableIndex specified\n";
528 SymbolTableIndex = *R.SymbolTableIndex;
529 } else {
530 SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
531 }
523532 OS << binary_le(R.VirtualAddress)
524533 << binary_le(SymbolTableIndex)
525534 << binary_le(R.Type);