Commit Graph

60 Commits

Author SHA1 Message Date
Xiaotian Wu
feedeaa339 Make loongarch64 use efi-app-loongarch64
Binutils 2.41 now supports efi-app-loongarch64, so to use this we must add a
dummy reloc section and remove the PE header definition from crt0 as this will
conflict with the version added by objcopy

Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
2024-03-22 10:24:23 -04:00
Callum Farmer
fb58e92621 ARM32: Only use 4K pages
Turns out some linkers have set page size to 64K on ARM32 aswell
so apply the same fix as ARM64

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:24:19 -04:00
Callum Farmer
fb3b3f4c66 riscv64: ignore unknown relocs
Sometimes ld emits relocs such as R_RISCV_64 for unwind symbols
these don't need to be handled yet so just can be skipped otherwise
the binary will never load

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:08:37 -04:00
Callum Farmer
4cdcf2a37b riscv64: fix image
- Make it actually work
- Split text/data/reloc/rodata sections
- Move hash sections to past end of image
  (un-needed in PE)
- Correct section & file alignment
- Correct image size

Builds correctly but untested

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:08:24 -04:00
Callum Farmer
eadee98bf0 ARM32: fix image
- Make it actually work
- Split text/data/reloc/rodata sections
- Move hash sections to past end of image
  (un-needed in PE)
- Correct section & file alignment
- Correct image size
- rename .reloc to .areloc within ELF
  as only on ARM32 does this name cause
  it to be marked as REL and subsequently
  mistakenly added to RELSZ

Builds correctly & tested working in QEMU

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:07:56 -04:00
Callum Farmer
0bb329b0bf Merge .plt into .text
ld writes .plt immediately after .text
which corrupts the alignment of .reloc

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:07:53 -04:00
Callum Farmer
262c1f4604 Split out rodata
rodata should be split for security

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:07:49 -04:00
Callum Farmer
0209271936 Clean up size symbols
Tells ld to define them after their markers

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:06:43 -04:00
Callum Farmer
db870d96d6 Mark _start as function
Currently marked as NOTYPE

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:06:43 -04:00
Callum Farmer
18d53408ff ARM32: _start must be global
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:06:43 -04:00
Callum Farmer
2dcebec0ce Add full Data Directories table
Microsoft dumpbin doesn't like the binary if
the full table doesn't exist

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:06:43 -04:00
Callum Farmer
bfa3cead95 ARM32: fix L_DYNAMIC
L_DYNAMIC must be 32bits to be PCREL

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:06:43 -04:00
Callum Farmer
dd82a45499 riscv: Use new ctors _entry func as entry point
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:06:43 -04:00
Callum Farmer
6c142a6df4 riscv64 lds: fix ctors/dtors
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:06:43 -04:00
Callum Farmer
a68997b11e riscv: Avoid TEXTREL
Use expression instead of variable for _data_size

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:06:38 -04:00
Callum Farmer
6f0648992a Avoid processor dependent sizes in assembly
.word -> .2byte
.short -> .2byte
.long -> .4byte
.quad -> .8byte

Reference: b83f0845b5
Fixes: https://sourceforge.net/p/gnu-efi/bugs/37/

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2024-03-22 10:05:31 -04:00
Callum Farmer
9dbfe1c365 Align all ctors/dtors start to 16
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-05-07 16:43:19 +01:00
Callum Farmer
9c5403e1e6 Copy marker names from binutils for init/fini
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-05-04 14:29:47 +01:00
Callum Farmer
e2aa099738 lds: Support ctors/dtors with a priority
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-05-04 14:29:36 +01:00
Callum Farmer
4c938fac30 Merge ctors/dtors rela sections
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-05-04 13:59:02 +01:00
Callum Farmer
bf6fd0405d ia32: No sdata section
There is no small data section on ia32,
this ends up being created as ld puts .got/.got.plt
here due to first declaration

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-04-21 13:38:17 +01:00
Callum Farmer
63a9a4d4c0 Add pkgconfig file
Also add EXEC_PREFIX and INCLUDEDIR variables

This was requested by fwupd-efi, to be able
to retrieve the version number of gnu-efi
so we can detect features

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-04-07 13:51:28 +01:00
Peter Jones
4f8b339fac Make ELF constructors and destructors work
This makes setup and teardown functions defined with
__attribute__((__constructor__) and __attribute__((__destructor__)) work
in normal circumstances in EFI binaries.

A couple of notes:
- it implements both the old-style .ctors/.dtors methods and the newer
  style .init_array/.fini_array ELF constructor and destructor arrays,
  processed in the order:
    .init_array[]
    .ctors[]
    efi_main()
    .dtors[]
    .fini_array[]
- Destructors will only be called if efi_main() exits using "return";
  any call to Exit() will still longjmp() past them.
- InitializeLib() has already been called before constructors run, so
  they don't need to call it (and neither does anything else.)  For
  compatibility, it has been altered so calling it more than once is
  safe.
- No attempt is made to handle any constructor or destructor with a
  prototype other than "void func(void);", but note that InitializeLib
  has been called, so LibImageHandle, ST, BS, and RT are set.
- The init_array/ctor/dtor/fini_array lists aren't the using the GNU
  "CONSTRUCTOR" output section command, so they don't start with a size.
- The lists are individually sorted during the link stage via
  SORT_BY_NAME() in the linker script.
- The default (empty) init_array/ctor/dtor/fini_array lists are padded
  out to 8-byte alignment with ".p2align 3, 0", and each list always has
  at least one ".long 0" at the end of it (even if it's completely
  empty).  As a result, they can have NULLs that need to be skipped.
  The sections they're in are mergeable, so the NULLs don't have to be
  exclusively at the end.
- The ia64 and mips64el arches have not been tested.

Signed-off-by: Peter Jones <pjones@redhat.com>
2023-03-28 08:59:41 -04:00
b'Nigel Croxon
5e2444d115 Merge /u/gmbr3/gnu-efi/ branch gc-sections into master
https://sourceforge.net/p/gnu-efi/code/merge-requests/42/
2023-03-23 14:17:28 +00:00
Callum Farmer
bc233a7a4b Prevent deletion of .reloc by gc-sections
Add keyword KEEP to prevent .reloc being removed
when using --gc-sections with GNU ld

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-03-22 15:32:24 +00:00
Callum Farmer
7ad75d9162 Ensure objcopy sections are aligned
Ensure the main sections are page aligned:
 - .text
 - .reloc
 - .dynamic
 - .data
 - .rel
 - .rel.plt

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-03-09 19:52:44 +00:00
bmeng@tinylab.org
0f9c15fe4f riscv: Update SPDX license identifiers
The whole gnu-efi project is licensed under BSD license, see [1].
However some of the RISC-V codes have conflict license identifiers:

- Some mention GPL-2.0+ in the SPDX license part, but the long license
  header indicates it's actually BSD and GPL-2.0+ dual-licensed
- Some mention GPL-2.0+ in the SPDX license part only
- Some do not have any license indication

To have a matching license with the whole project, this commit updates
all RISC-V codes to have the correct SPDX license identifiers
(GPL-2.0+ or BSD dual-license).

Link: https://sourceforge.net/p/gnu-efi/ [1]
Signed-off-by: Bin Meng <bmeng@tinylab.org>
Acked-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
2023-03-08 10:32:49 -05:00
Callum Farmer
1a10c66182 Un-merge .rel.plt section
Merging this section causes an incorrect RELASZ value
2023-03-07 22:47:37 +00:00
b'Nigel Croxon
330c88aa0c Merge /u/gmbr3/gnu-efi/ branch function-sections into master
https://sourceforge.net/p/gnu-efi/code/merge-requests/38/
2023-03-07 13:45:45 +00:00
Callum Farmer
02f96e8a26 *.efi.lds: Add .rel.dyn section
LLVM LLD always calls it .rel[a].dyn no matter what
so add this to the .rel[a] section

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-03-04 17:57:39 +00:00
Callum Farmer
15f10734e7 Add some COMDAT sections
When using function/data sections option from
gcc, some additional un-packed sections get created
move these to there correct placement if not already
included

sections seen on x86/x86_64/aarch64:
.gcc_except_table.efi_main
.bss.debugging

.gcc_except_table.* only appears on objcopy archs

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-03-03 21:00:32 +00:00
b'Nigel Croxon
d78b78cb2e Merge /u/gmbr3/gnu-efi/ branch merge-rela2 into master
https://sourceforge.net/p/gnu-efi/code/merge-requests/37/
2023-02-22 13:42:44 +00:00
Callum Farmer
eea6d31a22 *.efi.lds: Merge all rela sections into one
Objcopy sometimes generates .rela.plt and .rela.data
sections for which their names are longer than the 8
characters limit of PE32

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-02-21 11:57:47 +00:00
Nigel Croxon
637b3bdb9b Author: Callum Farmer <gmbr3@opensuse.org>
Date:   Tue Feb 7 21:48:27 2023 +0000

    ARM32/x86_64 lds: Force data segment align to 4KiB

    Some newer versions of binutils can set invalid COMMONPAGESIZE
    and MAXPAGESIZE values on these architectures

    git fetch git.code.sf.net/u/gmbr3/gnu-efi arm-pagesize
    git merge 89d55b364d

    Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-02-08 09:25:01 -05:00
Callum Farmer
89d55b364d ARM32/x86_64 lds: Force data segment align to 4KiB
Some newer versions of binutils can set invalid COMMONPAGESIZE
and MAXPAGESIZE values on these architectures

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-02-07 21:53:29 +00:00
Callum Farmer
1e00550694 aarch64 lds: Force data segment align to 64KiB
Required by UEFI 2.10 AArch64 specification
https://uefi.org/specs/UEFI/2.10/02_Overview.html#aarch64-platforms

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-02-07 21:18:23 +00:00
b'Nigel Croxon
f7b2e1ff6d Merge /u/gmbr3/gnu-efi/ branch master into master
https://sourceforge.net/p/gnu-efi/code/merge-requests/34/
2023-01-20 12:41:14 +00:00
Callum Farmer
593e13937c Make aarch64 crt0/lds more like x86_64
Binutils 2.38 now supports efi-app-aarch64 so
to use this we must add a dummy reloc section
and remove the PE header definition from crt0
as this will conflict with the version added
by objcopy

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-01-19 17:22:19 +00:00
Callum Farmer
231c7632c0 *.efi.lds: Use proper data segment placement
Use ld commands DATA_SEGMENT_ALIGN and DATA_SEGMENT_END

Fixes binutils 2.39 warning:
/usr/bin/ld: warning: efi/fwup.so has a LOAD segment with RWX
permissions

Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
2023-01-19 17:22:13 +00:00
b'Nigel Croxon
c2102ae059 Merge /u/mingtaoxt/gnu-efi/ branch master into master
https://sourceforge.net/p/gnu-efi/code/merge-requests/29/
2022-11-16 20:56:17 +00:00
Letu Ren
98f1250742 riscv: Fix ld RWX warning
The current ld script places .dynamic section right after .text, which
results in that one LOAD segment contains both data and code. Binutils
2.39 and later will throw a warning if RWX segments exist. Code and
data should be placed in different LOAD segments with different
permissions. This patch adds a gap between data and code sections which
is taken from the default ld script in riscv64.

Co-developed-by: Moody Liu <mooodyhunter@outlook.com>
Signed-off-by: Moody Liu <mooodyhunter@outlook.com>
Signed-off-by: Letu Ren <fantasquex@gmail.com>
2022-10-10 11:03:20 +02:00
Sergei Trofimovich
803b49c40b */*.S: add non-executable GNU stack marking on ELF-linux
binutils-2.39 enabed a few warning by default
(https://sourceware.org/pipermail/binutils/2022-August/122246.html):

> The ELF linker will now generate a warning message if the stack is made executable.

Let's suppress the warnings in assembly files by adding non-executables
stack markings. This fixes at least systemd build which uses '-Wl,--fatal-warnings':

    https://github.com/systemd/systemd/issues/24226
2022-08-08 11:35:10 -04:00
zhoumingtao
5849eacdce use jr instead of jirl
jirl -> jr
2022-07-07 15:16:10 +08:00
zhoumingtao
e7db4418b1 Add support for LoongArch64 architecture 2021-12-30 15:39:47 +08:00
Alfonso Sánchez-Beato
31a6aab44c Set NumberOfSymbols to zero
Acoording to what the spec says about the number of sybols [1]:

"This value should be zero for an image because COFF debugging
information is deprecated."

Changing as if not zero it causes problems to llvm-objcopy. This
affects only the architectures where COFF is not supported by objcopy
and where we build the PE header via assembly code.

[1] https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
2021-07-29 11:38:16 +02:00
Marvin Häuser
b916d1e54e Fix dummy relocation block alignment
As per the PE format specification, relocation blocks must be aligned on 32-bit boundaries. Fix the dummy relocations to obey the alignment constraints.
2021-04-06 22:35:03 +02:00
Heinrich Schuchardt
b95f138fa1 Initial support for RISCV64
Add the RISCV64 architecture

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
2021-04-01 17:17:33 +02:00
Nigel Croxon
230c36c6aa gnuefi/Makefile uses a strange rule to create libgnuefi.a, which only works
because of a "make" builtin rule. This breaks make -r, and it is an
inefficient way to build, at least for multiple files.

I have made a git tree which includes a patch for this problem,
and also forces -r by adding it to MAKEFLAGS:

git://git.zytor.com/users/hpa/gnu-efi.git make-r-fixes

Signed-off-by: H. Peter Anvin <hpa@users.sourceforge.net>
Signed-off-by: Nigel Croxon <ncroxon@redhat.com>
2019-04-02 09:47:24 -04:00
Sergei Trofimovich
2cc0b085fb gnuefi: preserve .gnu.hash sections (unbreaks elilo on IA-64)
Gentoo has slightly modified linker defaults: --hash-style=gnu
This means all ELF files in system have '.gnu.hash' section
but no '.hash' section.

gnuefi's ldscript did not account for it and as a result
one symbol 'ImageBase' did not resolve locally for elilo.so
and caused 'elilo' to fail to load by ia64 EFI:
  Loading.: Gentoo (try new elilo)
  ImageAddress: pointer is outside of image
  ImageAddress: pointer is outside of image

Those two relocations come from crt0-efi-ia64.S PE32 entry point
fdescr:

```
    #define IMAGE_REL_BASED_DIR64<->10
    .section .reloc, "a"
    data4   _start_plabel // Page RVA
    data4   12            // Block Size (2*4+2*2)
    data2   (IMAGE_REL_BASED_DIR64<<12) +  0 // reloc for plabel's entry point
    data2   (IMAGE_REL_BASED_DIR64<<12) +  8 // reloc for plabel's global pointer
```

These refer ImageBase.

The change adds '.gnu.hash' collection (follows existing '.hash'
collection).

Tested on IA-64 by successfully booting elilo-3.16.

Bug: https://bugs.gentoo.org/575300
Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
2018-01-27 20:29:05 +00:00
Nigel Croxon
8581a58e5b For compatibility with an upcoming EDK2 feature that maps UEFI apps
using strict permissions, emit the code as split .text/.data with
R-X/RW- permissions, respectively.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Nigel Croxon <ncroxon@redhat.com>
2017-02-15 19:54:32 -05:00