Skip to content

Commit 1bf1964

Browse files
committedJan 22, 2019
[llvm-objcopy] [COFF] Implement --add-gnu-debuglink
Differential Revision: https://reviews.llvm.org/D57007 llvm-svn: 351801
1 parent 9ec18a3 commit 1bf1964

File tree

6 files changed

+137
-7
lines changed

6 files changed

+137
-7
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
RUN: yaml2obj %p/Inputs/x86_64-exe.yaml > %t.in123.exe
2+
3+
# Using a debuglink filename with a length that is a multiple of 4, to
4+
# showcase padding in CONTENTS below.
5+
6+
RUN: llvm-objcopy --add-gnu-debuglink=%t.in123.exe %t.in123.exe %t.out.exe
7+
RUN: llvm-readobj -sections %t.out.exe | FileCheck %s --check-prefix=SECTIONS
8+
RUN: llvm-objdump -s %t.out.exe | FileCheck %s --check-prefix=CONTENTS
9+
10+
# Show the last of the preexisting sections, which is used for choosing
11+
# a virtual address for the generated one.
12+
13+
SECTIONS: Section {
14+
SECTIONS: Number: 4
15+
SECTIONS-NEXT: Name: .pdata
16+
SECTIONS-NEXT: VirtualSize: 0x18
17+
SECTIONS-NEXT: VirtualAddress: 0x4000
18+
SECTIONS-NEXT: RawDataSize: 512
19+
SECTIONS: Section {
20+
SECTIONS-NEXT: Number: 5
21+
SECTIONS-NEXT: Name: .gnu_debuglink
22+
SECTIONS-NEXT: VirtualSize: 0x2C
23+
SECTIONS-NEXT: VirtualAddress: 0x5000
24+
SECTIONS-NEXT: RawDataSize: 512
25+
SECTIONS-NEXT: PointerToRawData:
26+
SECTIONS-NEXT: PointerToRelocations:
27+
SECTIONS-NEXT: PointerToLineNumbers:
28+
SECTIONS-NEXT: RelocationCount:
29+
SECTIONS-NEXT: LineNumberCount:
30+
SECTIONS-NEXT: Characteristics [ (0x42000040)
31+
SECTIONS-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
32+
SECTIONS-NEXT: IMAGE_SCN_MEM_DISCARDABLE (0x2000000)
33+
SECTIONS-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
34+
SECTIONS-NEXT: ]
35+
36+
# Note: The last 4 bytes here are the crc of the referenced file - if the
37+
# yaml2obj generated file changes, this crc changes.
38+
39+
CONTENTS: Contents of section .gnu_debuglink:
40+
CONTENTS: 40005000 6164642d 676e752d 64656275 676c696e add-gnu-debuglin
41+
CONTENTS: 40005010 6b2e7465 73742e74 6d702e69 6e313233 k.test.tmp.in123
42+
CONTENTS: 40005020 2e657865 00000000 7929adc3 .exe

‎llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "llvm/Object/Binary.h"
1818
#include "llvm/Object/COFF.h"
1919
#include "llvm/Support/Errc.h"
20+
#include "llvm/Support/JamCRC.h"
21+
#include "llvm/Support/Path.h"
2022
#include <cassert>
2123

2224
namespace llvm {
@@ -30,6 +32,61 @@ static bool isDebugSection(const Section &Sec) {
3032
return Sec.Name.startswith(".debug");
3133
}
3234

35+
static uint64_t getNextRVA(const Object &Obj) {
36+
if (Obj.getSections().empty())
37+
return 0;
38+
const Section &Last = Obj.getSections().back();
39+
return alignTo(Last.Header.VirtualAddress + Last.Header.VirtualSize,
40+
Obj.PeHeader.SectionAlignment);
41+
}
42+
43+
static uint32_t getCRC32(StringRef Data) {
44+
JamCRC CRC;
45+
CRC.update(ArrayRef<char>(Data.data(), Data.size()));
46+
// The CRC32 value needs to be complemented because the JamCRC dosn't
47+
// finalize the CRC32 value. It also dosn't negate the initial CRC32 value
48+
// but it starts by default at 0xFFFFFFFF which is the complement of zero.
49+
return ~CRC.getCRC();
50+
}
51+
52+
static std::vector<uint8_t> createGnuDebugLinkSectionContents(StringRef File) {
53+
ErrorOr<std::unique_ptr<MemoryBuffer>> LinkTargetOrErr =
54+
MemoryBuffer::getFile(File);
55+
if (!LinkTargetOrErr)
56+
error("'" + File + "': " + LinkTargetOrErr.getError().message());
57+
auto LinkTarget = std::move(*LinkTargetOrErr);
58+
uint32_t CRC32 = getCRC32(LinkTarget->getBuffer());
59+
60+
StringRef FileName = sys::path::filename(File);
61+
size_t CRCPos = alignTo(FileName.size() + 1, 4);
62+
std::vector<uint8_t> Data(CRCPos + 4);
63+
memcpy(Data.data(), FileName.data(), FileName.size());
64+
support::endian::write32le(Data.data() + CRCPos, CRC32);
65+
return Data;
66+
}
67+
68+
static void addGnuDebugLink(Object &Obj, StringRef DebugLinkFile) {
69+
uint32_t StartRVA = getNextRVA(Obj);
70+
71+
std::vector<Section> Sections;
72+
Section Sec;
73+
Sec.setOwnedContents(createGnuDebugLinkSectionContents(DebugLinkFile));
74+
Sec.Name = ".gnu_debuglink";
75+
Sec.Header.VirtualSize = Sec.getContents().size();
76+
Sec.Header.VirtualAddress = StartRVA;
77+
Sec.Header.SizeOfRawData =
78+
alignTo(Sec.Header.VirtualSize, Obj.PeHeader.FileAlignment);
79+
// Sec.Header.PointerToRawData is filled in by the writer.
80+
Sec.Header.PointerToRelocations = 0;
81+
Sec.Header.PointerToLinenumbers = 0;
82+
// Sec.Header.NumberOfRelocations is filled in by the writer.
83+
Sec.Header.NumberOfLinenumbers = 0;
84+
Sec.Header.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA |
85+
IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE;
86+
Sections.push_back(Sec);
87+
Obj.addSections(Sections);
88+
}
89+
3390
static Error handleArgs(const CopyConfig &Config, Object &Obj) {
3491
// Perform the actual section removals.
3592
Obj.removeSections([&Config](const Section &Sec) {
@@ -109,6 +166,10 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
109166

110167
return false;
111168
});
169+
170+
if (!Config.AddGnuDebugLink.empty())
171+
addGnuDebugLink(Obj, Config.AddGnuDebugLink);
172+
112173
return Error::success();
113174
}
114175

‎llvm/tools/llvm-objcopy/COFF/Object.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {
129129
void Object::truncateSections(function_ref<bool(const Section &)> ToTruncate) {
130130
for (Section &Sec : Sections) {
131131
if (ToTruncate(Sec)) {
132-
Sec.Contents = ArrayRef<uint8_t>();
132+
Sec.clearContents();
133133
Sec.Relocs.clear();
134134
Sec.Header.SizeOfRawData = 0;
135135
}

‎llvm/tools/llvm-objcopy/COFF/Object.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,35 @@ struct Relocation {
3535

3636
struct Section {
3737
object::coff_section Header;
38-
ArrayRef<uint8_t> Contents;
3938
std::vector<Relocation> Relocs;
4039
StringRef Name;
4140
ssize_t UniqueId;
4241
size_t Index;
42+
43+
ArrayRef<uint8_t> getContents() const {
44+
if (!OwnedContents.empty())
45+
return OwnedContents;
46+
return ContentsRef;
47+
}
48+
49+
void setContentsRef(ArrayRef<uint8_t> Data) {
50+
OwnedContents.clear();
51+
ContentsRef = Data;
52+
}
53+
54+
void setOwnedContents(std::vector<uint8_t> &&Data) {
55+
ContentsRef = ArrayRef<uint8_t>();
56+
OwnedContents = std::move(Data);
57+
}
58+
59+
void clearContents() {
60+
ContentsRef = ArrayRef<uint8_t>();
61+
OwnedContents.clear();
62+
}
63+
64+
private:
65+
ArrayRef<uint8_t> ContentsRef;
66+
std::vector<uint8_t> OwnedContents;
4367
};
4468

4569
struct Symbol {

‎llvm/tools/llvm-objcopy/COFF/Reader.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ Error COFFReader::readSections(Object &Obj) const {
6969
Sections.push_back(Section());
7070
Section &S = Sections.back();
7171
S.Header = *Sec;
72-
if (auto EC = COFFObj.getSectionContents(Sec, S.Contents))
72+
ArrayRef<uint8_t> Contents;
73+
if (auto EC = COFFObj.getSectionContents(Sec, Contents))
7374
return errorCodeToError(EC);
75+
S.setContentsRef(Contents);
7476
ArrayRef<coff_relocation> Relocs = COFFObj.getRelocations(Sec);
7577
for (const coff_relocation &R : Relocs)
7678
S.Relocs.push_back(R);

‎llvm/tools/llvm-objcopy/COFF/Writer.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,15 @@ void COFFWriter::writeHeaders(bool IsBigObj) {
286286
void COFFWriter::writeSections() {
287287
for (const auto &S : Obj.getSections()) {
288288
uint8_t *Ptr = Buf.getBufferStart() + S.Header.PointerToRawData;
289-
std::copy(S.Contents.begin(), S.Contents.end(), Ptr);
289+
ArrayRef<uint8_t> Contents = S.getContents();
290+
std::copy(Contents.begin(), Contents.end(), Ptr);
290291

291292
// For executable sections, pad the remainder of the raw data size with
292293
// 0xcc, which is int3 on x86.
293294
if ((S.Header.Characteristics & IMAGE_SCN_CNT_CODE) &&
294-
S.Header.SizeOfRawData > S.Contents.size())
295-
memset(Ptr + S.Contents.size(), 0xcc,
296-
S.Header.SizeOfRawData - S.Contents.size());
295+
S.Header.SizeOfRawData > Contents.size())
296+
memset(Ptr + Contents.size(), 0xcc,
297+
S.Header.SizeOfRawData - Contents.size());
297298

298299
Ptr += S.Header.SizeOfRawData;
299300
for (const auto &R : S.Relocs) {

0 commit comments

Comments
 (0)
Please sign in to comment.