return OffsetOfEntry;
}
-coff_symbol16 Writer::createSymbol(DefinedRegular *D) {
- uint64_t RVA = D->getRVA();
- OutputSection *Sec = nullptr;
- for (OutputSection *S : OutputSections) {
- if (S->getRVA() > RVA)
- break;
- Sec = S;
- }
+Optional<coff_symbol16> Writer::createSymbol(Defined *Def) {
+ if (auto *D = dyn_cast<DefinedRegular>(Def))
+ if (!D->isLive())
+ return None;
coff_symbol16 Sym;
- StringRef Name = D->getName();
+ StringRef Name = Def->getName();
if (Name.size() > COFF::NameSize) {
Sym.Name.Offset.Zeroes = 0;
Sym.Name.Offset.Offset = addEntryToStringTable(Name);
memset(Sym.Name.ShortName, 0, COFF::NameSize);
memcpy(Sym.Name.ShortName, Name.data(), Name.size());
}
- COFFSymbolRef DSymRef = D->getCOFFSymbol();
- Sym.Value = RVA - Sec->getRVA();
- Sym.SectionNumber = Sec->SectionIndex;
- Sym.Type = DSymRef.getType();
- Sym.StorageClass = DSymRef.getStorageClass();
+
+ if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
+ COFFSymbolRef Ref = D->getCOFFSymbol();
+ Sym.Type = Ref.getType();
+ Sym.StorageClass = Ref.getStorageClass();
+ } else {
+ Sym.Type = IMAGE_SYM_TYPE_NULL;
+ Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
+ }
Sym.NumberOfAuxSymbols = 0;
+
+ switch (Def->kind()) {
+ case SymbolBody::DefinedAbsoluteKind:
+ case SymbolBody::DefinedRelativeKind:
+ Sym.Value = Def->getRVA();
+ Sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
+ break;
+ default: {
+ uint64_t RVA = Def->getRVA();
+ OutputSection *Sec = nullptr;
+ for (OutputSection *S : OutputSections) {
+ if (S->getRVA() > RVA)
+ break;
+ Sec = S;
+ }
+ Sym.Value = RVA - Sec->getRVA();
+ Sym.SectionNumber = Sec->SectionIndex;
+ break;
+ }
+ }
return Sym;
}
for (ObjectFile *File : Symtab->ObjectFiles)
for (SymbolBody *B : File->getSymbols())
- if (auto *D = dyn_cast<DefinedRegular>(B))
- if (D->isLive())
- OutputSymtab.push_back(createSymbol(D));
+ if (auto *D = dyn_cast<Defined>(B))
+ if (Optional<coff_symbol16> Sym = createSymbol(D))
+ OutputSymtab.push_back(*Sym);
+
+ for (ImportFile *File : Symtab->ImportFiles)
+ for (SymbolBody *B : File->getSymbols())
+ if (Optional<coff_symbol16> Sym = createSymbol(cast<Defined>(B)))
+ OutputSymtab.push_back(*Sym);
OutputSection *LastSection = OutputSections.back();
// We position the symbol table to be adjacent to the end of the last section.
# RUN: yaml2obj < %s > %t.obj
-# RUN: lld -flavor link2 /debug /out:%t.exe /entry:main %t.obj
+# RUN: lld -flavor link2 /debug /out:%t.exe /entry:main %t.obj %p/Inputs/std64.lib
# RUN: llvm-readobj -symbols %t.exe | FileCheck %s
# CHECK: Symbols [
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: .text
# CHECK-NEXT: Value: 0
-# CHECK-NEXT: Section: .text
+# CHECK-NEXT: Section: .text (2)
# CHECK-NEXT: BaseType: Null (0x0)
# CHECK-NEXT: ComplexType: Null (0x0)
-# CHECK-NEXT: StorageClass: Static
+# CHECK-NEXT: StorageClass: Static (0x3)
# CHECK-NEXT: AuxSymbolCount: 0
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
-# CHECK-NEXT: Name: f
-# CHECK-NEXT: Value: 2
-# CHECK-NEXT: Section: .text
+# CHECK-NEXT: Name: .text2
+# CHECK-NEXT: Value: 0
+# CHECK-NEXT: Section: .text (2)
+# CHECK-NEXT: BaseType: Null (0x0)
+# CHECK-NEXT: ComplexType: Null (0x0)
+# CHECK-NEXT: StorageClass: Static (0x3)
+# CHECK-NEXT: AuxSymbolCount: 0
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: .data
+# CHECK-NEXT: Value: 0
+# CHECK-NEXT: Section: .data (1)
# CHECK-NEXT: BaseType: Null (0x0)
# CHECK-NEXT: ComplexType: Null (0x0)
-# CHECK-NEXT: StorageClass: External
+# CHECK-NEXT: StorageClass: Static (0x3)
# CHECK-NEXT: AuxSymbolCount: 0
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
-# CHECK-NEXT: Name: g
-# CHECK-NEXT: Value: 4
-# CHECK-NEXT: Section: .text
+# CHECK-NEXT: Name: message
+# CHECK-NEXT: Value: 6
+# CHECK-NEXT: Section: .text2 (3)
# CHECK-NEXT: BaseType: Null (0x0)
# CHECK-NEXT: ComplexType: Null (0x0)
-# CHECK-NEXT: StorageClass: External
+# CHECK-NEXT: StorageClass: Static (0x3)
# CHECK-NEXT: AuxSymbolCount: 0
# CHECK-NEXT: }
# CHECK-NEXT: Symbol {
# CHECK-NEXT: Name: main
# CHECK-NEXT: Value: 0
-# CHECK-NEXT: Section: .text
+# CHECK-NEXT: Section: .text (2)
+# CHECK-NEXT: BaseType: Null (0x0)
+# CHECK-NEXT: ComplexType: Null (0x0)
+# CHECK-NEXT: StorageClass: External (0x2)
+# CHECK-NEXT: AuxSymbolCount: 0
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: caption
+# CHECK-NEXT: Value: 0
+# CHECK-NEXT: Section: .text2 (3)
+# CHECK-NEXT: BaseType: Null (0x0)
+# CHECK-NEXT: ComplexType: Null (0x0)
+# CHECK-NEXT: StorageClass: Static (0x3)
+# CHECK-NEXT: AuxSymbolCount: 0
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: abs_symbol
+# CHECK-NEXT: Value: 2662186735
+# CHECK-NEXT: Section: IMAGE_SYM_ABSOLUTE (-1)
# CHECK-NEXT: BaseType: Null (0x0)
# CHECK-NEXT: ComplexType: Null (0x0)
-# CHECK-NEXT: StorageClass: External
+# CHECK-NEXT: StorageClass: External (0x2)
+# CHECK-NEXT: AuxSymbolCount: 0
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: __imp_ExitProcess
+# CHECK-NEXT: Value: 64
+# CHECK-NEXT: Section: .idata (4)
+# CHECK-NEXT: BaseType: Null (0x0)
+# CHECK-NEXT: ComplexType: Null (0x0)
+# CHECK-NEXT: StorageClass: External (0x2)
+# CHECK-NEXT: AuxSymbolCount: 0
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: ExitProcess
+# CHECK-NEXT: Value: 64
+# CHECK-NEXT: Section: .text (2)
+# CHECK-NEXT: BaseType: Null (0x0)
+# CHECK-NEXT: ComplexType: Null (0x0)
+# CHECK-NEXT: StorageClass: External (0x2)
+# CHECK-NEXT: AuxSymbolCount: 0
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: __imp_MessageBoxA
+# CHECK-NEXT: Value: 72
+# CHECK-NEXT: Section: .idata (4)
+# CHECK-NEXT: BaseType: Null (0x0)
+# CHECK-NEXT: ComplexType: Null (0x0)
+# CHECK-NEXT: StorageClass: External (0x2)
+# CHECK-NEXT: AuxSymbolCount: 0
+# CHECK-NEXT: }
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT: Name: MessageBoxA
+# CHECK-NEXT: Value: 80
+# CHECK-NEXT: Section: .text (2)
+# CHECK-NEXT: BaseType: Null (0x0)
+# CHECK-NEXT: ComplexType: Null (0x0)
+# CHECK-NEXT: StorageClass: External (0x2)
# CHECK-NEXT: AuxSymbolCount: 0
# CHECK-NEXT: }
# CHECK-NEXT: ]
---
header:
Machine: IMAGE_FILE_MACHINE_AMD64
- Characteristics: [ ]
+ Characteristics: []
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+ Alignment: 4096
+ SectionData: B800000000000000005068000000000000000068000000000000000050E8000000000000000050E8000000000000000050E80000000000000000
+ Relocations:
+ - VirtualAddress: 0
+ SymbolName: abs_symbol
+ Type: IMAGE_REL_AMD64_ADDR64
+ - VirtualAddress: 7
+ SymbolName: caption
+ Type: IMAGE_REL_AMD64_ADDR64
+ - VirtualAddress: 12
+ SymbolName: message
+ Type: IMAGE_REL_AMD64_ADDR64
+ - VirtualAddress: 18
+ SymbolName: MessageBoxA
+ Type: IMAGE_REL_AMD64_REL32
+ - VirtualAddress: 24
+ SymbolName: ExitProcess
+ Type: IMAGE_REL_AMD64_REL32
+ - VirtualAddress: 30
+ SymbolName: __ImageBase
+ Type: IMAGE_REL_AMD64_ADDR64
+ - Name: .text2
+ Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+ Alignment: 4096
+ SectionData: B800000000000000005068000000000000000068000000000000000050E8000000000000000050E8000000000000000050E80000000000000000
+ Relocations:
+ - VirtualAddress: 0
+ SymbolName: abs_symbol
+ Type: IMAGE_REL_AMD64_ADDR64
+ - VirtualAddress: 7
+ SymbolName: caption
+ Type: IMAGE_REL_AMD64_ADDR64
+ - VirtualAddress: 12
+ SymbolName: message
+ Type: IMAGE_REL_AMD64_ADDR64
+ - VirtualAddress: 18
+ SymbolName: MessageBoxA
+ Type: IMAGE_REL_AMD64_REL32
+ - VirtualAddress: 24
+ SymbolName: ExitProcess
+ Type: IMAGE_REL_AMD64_REL32
+ - VirtualAddress: 30
+ SymbolName: __ImageBase
+ Type: IMAGE_REL_AMD64_ADDR64
+ - Name: .data
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
Alignment: 4
- SectionData: B82A00
+ SectionData: 48656C6C6F0048656C6C6F20576F726C6400
symbols:
+ - Name: "@comp.id"
+ Value: 10394907
+ SectionNumber: 65535
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
- Name: .text
Value: 0
SectionNumber: 1
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
- Length: 6
- NumberOfRelocations: 0
+ Length: 28
+ NumberOfRelocations: 6
NumberOfLinenumbers: 0
CheckSum: 0
Number: 0
- - Name: f
- Value: 2
+ - Name: .text2
+ Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 28
+ NumberOfRelocations: 6
+ NumberOfLinenumbers: 0
+ CheckSum: 0
+ Number: 0
+ - Name: .data
+ Value: 0
+ SectionNumber: 3
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 18
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 0
+ Number: 0
+ - Name: MessageBoxA
+ Value: 0
+ SectionNumber: 0
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- - Name: g
- Value: 4
- SectionNumber: 1
+ - Name: ExitProcess
+ Value: 0
+ SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+ - Name: message
+ Value: 6
+ SectionNumber: 2
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
- Name: main
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+ - Name: caption
+ Value: 0
+ SectionNumber: 2
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ - Name: abs_symbol
+ Value: 0xDEADBEEF
+ SectionNumber: -1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+ - Name: __ImageBase
+ Value: 0
+ SectionNumber: 0
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
...