Skip to content

Commit

Permalink
Merging r352082:
Browse files Browse the repository at this point in the history
------------------------------------------------------------------------
r352082 | ruiu | 2019-01-24 20:02:31 +0100 (Thu, 24 Jan 2019) | 15 lines

Fix broken export table if .rdata is merged with .text.

Previously, we assumed that .rdata is zero-filled, so when writing
an COFF import table, we didn't write anything if the data is zero.
That assumption was wrong because .rdata can be merged with .text.
If .rdata is merged with .text, they are initialized with 0xcc which
is a trap instruction.

This patch removes that assumption from code.

Should be merged to 8.0 branch as this is a regression.

Fixes https://bugs.llvm.org/show_bug.cgi?id=39826

Differential Revision: https://reviews.llvm.org/D57168
------------------------------------------------------------------------

llvm-svn: 352135
  • Loading branch information
zmodem committed Jan 25, 2019
1 parent 24479b1 commit 22f92b5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lld/COFF/DLL.cpp
Expand Up @@ -47,6 +47,7 @@ class HintNameChunk : public Chunk {
}

void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, getSize());
write16le(Buf + OutputSectionOff, Hint);
memcpy(Buf + OutputSectionOff + 2, Name.data(), Name.size());
}
Expand All @@ -63,7 +64,10 @@ class LookupChunk : public Chunk {
size_t getSize() const override { return Config->Wordsize; }

void writeTo(uint8_t *Buf) const override {
write32le(Buf + OutputSectionOff, HintName->getRVA());
if (Config->is64())
write64le(Buf + OutputSectionOff, HintName->getRVA());
else
write32le(Buf + OutputSectionOff, HintName->getRVA());
}

Chunk *HintName;
Expand Down Expand Up @@ -99,6 +103,8 @@ class ImportDirectoryChunk : public Chunk {
size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }

void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, getSize());

auto *E = (coff_import_directory_table_entry *)(Buf + OutputSectionOff);
E->ImportLookupTableRVA = LookupTab->getRVA();
E->NameRVA = DLLName->getRVA();
Expand All @@ -118,6 +124,10 @@ class NullChunk : public Chunk {
bool hasData() const override { return false; }
size_t getSize() const override { return Size; }

void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, Size);
}

private:
size_t Size;
};
Expand Down Expand Up @@ -160,6 +170,8 @@ class DelayDirectoryChunk : public Chunk {
}

void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, getSize());

auto *E = (delay_import_directory_table_entry *)(Buf + OutputSectionOff);
E->Attributes = 1;
E->Name = DLLName->getRVA();
Expand Down Expand Up @@ -392,6 +404,8 @@ class ExportDirectoryChunk : public Chunk {
}

void writeTo(uint8_t *Buf) const override {
memset(Buf + OutputSectionOff, 0, getSize());

auto *E = (export_directory_table_entry *)(Buf + OutputSectionOff);
E->NameRVA = DLLName->getRVA();
E->OrdinalBase = 0;
Expand Down
13 changes: 13 additions & 0 deletions lld/test/COFF/imports.test
Expand Up @@ -34,3 +34,16 @@ IMPORT-NEXT: Symbol: ExitProcess (0)
IMPORT-NEXT: Symbol: (50)
IMPORT-NEXT: Symbol: MessageBoxA (1)
IMPORT-NEXT: }

# RUN: lld-link /out:%t.exe /entry:main /subsystem:console /merge:.rdata=.text \
# RUN: %p/Inputs/hello64.obj %p/Inputs/std64.lib /include:ExitProcess
# RUN: llvm-readobj -coff-imports %t.exe | FileCheck -check-prefix=MERGE %s

MERGE: Import {
MERGE-NEXT: Name: std64.dll
MERGE-NEXT: ImportLookupTableRVA: 0x1090
MERGE-NEXT: ImportAddressTableRVA: 0x10B0
MERGE-NEXT: Symbol: ExitProcess (0)
MERGE-NEXT: Symbol: (50)
MERGE-NEXT: Symbol: MessageBoxA (1)
MERGE-NEXT: }

0 comments on commit 22f92b5

Please sign in to comment.