Files
clang-p2996/lld/test/ELF/linkerscript/sections-keep.s
Rui Ueyama 3da3f06dd3 Include version string into ".comment" section.
Summary:
This patch adds a ".comment" section to an output. The comment
section contains the linker's version string. You can now
find out whether a binary is created by LLD or not using objdump
command like this.

  $ objdump -s -j .comment foo

  foo:     file format elf64-x86-64

  Contents of section .comment:
   0000 00474343 3a202855 62756e74 7520342e  .GCC: (Ubuntu 4.
   0010 382e342d 32756275 6e747531 7e31342e  8.4-2ubuntu1~14.
   ...
   00c0 766d2f74 72756e6b 20323835 38343629  vm/trunk 285846)
   00d0 004c696e 6b65723a 204c4c44 20342e30  .Linker: LLD 4.0
   00e0 2e302028 7472756e 6b203238 36343036  .0 (trunk 286406
   00f0 2900                                 ).

Compilers emits .comment section as well, so the output contains
both compiler and linker information.

Alternative considered:

I first tried to add a SHT_NOTE section because GNU gold does that.
A NOTE section starts with a header which contains content type.
It turned out that ld.gold sets type NT_GNU_GOLD_VERSION to their
NOTE section. So the NOTE type is only for GNU gold (surprise!)

Next, I tried to create ".linker-version" section. However, it seems
that reusing the existing ".comment" section is better because 1)
other tools already know about .comment section and is able to strip
it and 2) the result contans not only linker info but also compiler
info.

Differential Revision: https://reviews.llvm.org/D26487

llvm-svn: 286496
2016-11-10 20:20:37 +00:00

96 lines
3.7 KiB
ArmAsm

# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/keep.s -o %t2.o
## First check that section "keep" is garbage collected without using KEEP
# RUN: echo "SECTIONS { \
# RUN: .text : { *(.text) } \
# RUN: .keep : { *(.keep) } \
# RUN: .temp : { *(.temp) }}" > %t.script
# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t
# RUN: llvm-objdump -section-headers %t1 | \
# RUN: FileCheck -check-prefix=SECGC %s
# SECGC: Sections:
# SECGC-NEXT: Idx Name Size
# SECGC-NEXT: 0 00000000
# SECGC-NEXT: 1 .text 00000007
# SECGC-NEXT: 2 .temp 00000004
## Now apply KEEP command to preserve the section.
# RUN: echo "SECTIONS { \
# RUN: .text : { *(.text) } \
# RUN: .keep : { KEEP(*(.keep)) } \
# RUN: .temp : { *(.temp) }}" > %t.script
# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t
# RUN: llvm-objdump -section-headers %t1 | \
# RUN: FileCheck -check-prefix=SECNOGC %s
# SECNOGC: Sections:
# SECNOGC-NEXT: Idx Name Size
# SECNOGC-NEXT: 0 00000000
# SECNOGC-NEXT: 1 .text 00000007
# SECNOGC-NEXT: 2 .keep 00000004
# SECNOGC-NEXT: 3 .temp 00000004
## A section name matches two entries in the SECTIONS directive. The
## first one doesn't have KEEP, the second one does. If section that have
## KEEP is the first in order then section is NOT collected.
# RUN: echo "SECTIONS { \
# RUN: . = SIZEOF_HEADERS; \
# RUN: .keep : { KEEP(*(.keep)) } \
# RUN: .nokeep : { *(.keep) }}" > %t.script
# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t
# RUN: llvm-objdump -section-headers %t1 | FileCheck -check-prefix=MIXED1 %s
# MIXED1: Sections:
# MIXED1-NEXT: Idx Name Size
# MIXED1-NEXT: 0 00000000
# MIXED1-NEXT: 1 .keep 00000004
# MIXED1-NEXT: 2 .text 00000007 00000000000000ec TEXT DATA
# MIXED1-NEXT: 3 .temp 00000004 00000000000000f3 DATA
# MIXED1-NEXT: 4 .comment 00000008 0000000000000000
# MIXED1-NEXT: 5 .symtab 00000060 0000000000000000
# MIXED1-NEXT: 6 .shstrtab 00000036 0000000000000000
# MIXED1-NEXT: 7 .strtab 00000012 0000000000000000
## The same, but now section without KEEP is at first place.
## gold and bfd linkers disagree here. gold collects .keep while
## bfd keeps it. Our current behavior is compatible with bfd although
## we can choose either way.
# RUN: echo "SECTIONS { \
# RUN: . = SIZEOF_HEADERS; \
# RUN: .nokeep : { *(.keep) } \
# RUN: .keep : { KEEP(*(.keep)) }}" > %t.script
# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t
# RUN: llvm-objdump -section-headers %t1 | FileCheck -check-prefix=MIXED2 %s
# MIXED2: Sections:
# MIXED2-NEXT: Idx Name Size
# MIXED2-NEXT: 0 00000000
# MIXED2-NEXT: 1 .nokeep 00000004 00000000000000e8 DATA
# MIXED2-NEXT: 2 .text 00000007 00000000000000ec TEXT DATA
# MIXED2-NEXT: 3 .temp 00000004 00000000000000f3 DATA
# MIXED2-NEXT: 4 .comment 00000008 0000000000000000
# MIXED2-NEXT: 5 .symtab 00000060 0000000000000000
# MIXED2-NEXT: 6 .shstrtab 00000038 0000000000000000
# MIXED2-NEXT: 7 .strtab 00000012 0000000000000000
# Check file pattern for kept sections.
# RUN: echo "SECTIONS { \
# RUN: . = SIZEOF_HEADERS; \
# RUN: .keep : { KEEP(*2.o(.keep)) } \
# RUN: }" > %t.script
# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t2.o %t
# RUN: llvm-objdump -s %t1 | FileCheck -check-prefix=FILEMATCH %s
# FILEMATCH: Contents of section .keep:
# FILEMATCH-NEXT: 00e8 41414141 AAAA
.global _start
_start:
mov temp, %eax
.section .keep, "a"
keep:
.long 1
.section .temp, "a"
temp:
.long 2