From 2c8f1f048c97ffdeb34b8e8723e9a7369d2987ee Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 29 Aug 2016 22:01:21 +0000 Subject: [PATCH] Make lld actually compatible with gold in terms of filler handling. GNU gold handles output section fillers as 32-bit values. This patch makes LLD compatible with that behavior. Differential revision: https://reviews.llvm.org/D23181 llvm-svn: 280018 --- lld/ELF/LinkerScript.cpp | 20 ++++++++++---------- .../ELF/linkerscript/linkerscript-sections-padding.s | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index c00791d..060844e 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -956,24 +956,24 @@ ScriptParser::readOutputSectionDescription(StringRef OutSec) { return Cmd; } +// Read "=" where is an octal/decimal/hexadecimal number. +// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html +// +// ld.gold is not fully compatible with ld.bfd. ld.bfd handles +// hexstrings as blobs of arbitrary sizes, while ld.gold handles them +// as 32-bit big-endian values. We will do the same as ld.gold does +// because it's simpler than what ld.bfd does. std::vector ScriptParser::readOutputSectionFiller() { - StringRef Tok = peek(); - if (!Tok.startswith("=")) + if (!peek().startswith("=")) return {}; - next(); - // Read a hexstring of arbitrary length. - if (Tok.startswith("=0x")) - return parseHex(Tok.substr(3)); - - // Read a decimal or octal value as a big-endian 32 bit value. - // Why do this? I don't know, but that's what gold does. + StringRef Tok = next(); uint32_t V; if (Tok.substr(1).getAsInteger(0, V)) { setError("invalid filler expression: " + Tok); return {}; } - return { uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V) }; + return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)}; } SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) { diff --git a/lld/test/ELF/linkerscript/linkerscript-sections-padding.s b/lld/test/ELF/linkerscript/linkerscript-sections-padding.s index ba3a95f..eb7c8d6 100644 --- a/lld/test/ELF/linkerscript/linkerscript-sections-padding.s +++ b/lld/test/ELF/linkerscript/linkerscript-sections-padding.s @@ -2,16 +2,16 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t ## Check that padding value works: -# RUN: echo "SECTIONS { .mysec : { *(.mysec*) } =0x112233445566778899 }" > %t.script +# RUN: echo "SECTIONS { .mysec : { *(.mysec*) } =0x1122 }" > %t.script # RUN: ld.lld -o %t.out --script %t.script %t # RUN: llvm-objdump -s %t.out | FileCheck -check-prefix=YES %s -# YES: 0120 66223344 55667788 99112233 44556677 +# YES: 0120 66001122 00001122 00001122 00001122 ## Confirming that address was correct: -# RUN: echo "SECTIONS { .mysec : { *(.mysec*) } =0x998877665544332211 }" > %t.script +# RUN: echo "SECTIONS { .mysec : { *(.mysec*) } =0x99887766 }" > %t.script # RUN: ld.lld -o %t.out --script %t.script %t # RUN: llvm-objdump -s %t.out | FileCheck -check-prefix=YES2 %s -# YES2: 0120 66887766 55443322 11998877 66554433 +# YES2: 0120 66887766 99887766 99887766 99887766 ## Default padding value is 0x00: # RUN: echo "SECTIONS { .mysec : { *(.mysec*) } }" > %t.script @@ -29,7 +29,7 @@ # RUN: echo "SECTIONS { .mysec : { *(.mysec*) } =0x99XX }" > %t.script # RUN: not ld.lld -o %t.out --script %t.script %t 2>&1 \ # RUN: | FileCheck --check-prefix=ERR2 %s -# ERR2: not a hexadecimal value: XX +# ERR2: invalid filler expression: =0x99XX .section .mysec.1,"a" .align 16 -- 2.7.4