1
0
mirror of https://github.com/systemd/systemd synced 2024-07-09 04:26:06 +00:00
Commit Graph

19 Commits

Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek
125f06dd1c tools/elf2efi: elif→if to make pylint happy 2024-03-22 15:44:17 +01:00
Zbigniew Jędrzejewski-Szmek
6d03e5523c tools/elf2efi: skip empty .got section and its .relro_padding
Resolves https://github.com/systemd/systemd/issues/31637.

lld-18 does the section setup differently than older versions. There is a bunch
of ordering chagnes, but it also inserts the following:

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
...
  9 .got          00000000  00000000000283c0  00000000000283c0  000283c0  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 10 .relro_padding 00000c40  00000000000283c0  00000000000283c0  000283c0  2**0
                  ALLOC
 11 .data         00000024  00000000000293c0  00000000000293c0  000283c0  2**4
                  CONTENTS, ALLOC, LOAD, DATA
...

This causes a problem for us, because we try to map the .got to .rodata,
and the subsequent .data to .data, and round down the VMA to the nearest
page, which causes the PE sections to overlap.

https://github.com/llvm/llvm-project/pull/66042 adds .relro_padding to make
sure that the RELRO segment is properly write protected and allocated. For our
binaries, the .got section is empty, so we can skip it safely, and the
.relro_padding section is not useful once .got has been dropped.

We don't expect .got sections, but they are apparently inserted on i386 and
aarch64 builds. Emit a warning until we figure out why they are there.
2024-03-22 15:42:57 +01:00
Zbigniew Jędrzejewski-Szmek
8a753717f8 tools/elf2efi: rework exception messages
RuntimeError is documented as "Unspecified run-time error". It doesn't make
much sense for Python. (It originated in Java, where exceptions that can be
thrown by a function are declared in the function signature. All code calling
such a function must either explicitly catch all possible exception types, or
allow them to propagate by listing them in its own exception type list. This is
nice in theory, but in practice very annoying. Especially during development,
when the list of possible exception types is not finalized, we would end up
adding and removing exceptions to functions signatures all the time. Also for
code which is designed to call functions recursively, we would soon end up with
all functions declaring all possible exception types… To avoid this, people
would quite often do fake handling with a block that either prints and ignores
an exception, or has just a comment like "fix me later", or even nothing. This
often lead to people forgetting to adjust this later on and production code
containing such constructs. An escape hatch was opened with RuntimeException and
its subclasses, which do not need to be pre-declared. Various memory-related
exceptions were added as subclasses of RuntimeException. But later on, people
starting using this to not to have to declare all exception types everywhere.)

In Python, exceptions do no have to be pre-declared, and for code which just
encounters a failure, we should raise a specific exception type. The catch-all
class for unexpected input is ValueError.

For https://github.com/systemd/systemd/issues/31637:
BadSectionError: Section '.data' @0x28000 overlaps previous section @0x28000+0x300=@0x28300

Also, exception strings should not contain trailing periods, because they are
often embedded in sentences.
2024-03-14 11:08:41 +01:00
Zbigniew Jędrzejewski-Szmek
642f991b70 tools/elf2efi: split out function to create parser
main() is supposed to be lean and mean.
2024-03-14 10:32:17 +01:00
Zbigniew Jędrzejewski-Szmek
a0797b4ad7 tools/elf2efi: align columns in tables, unify formatting
For tables which represent binary data structures, readability is greatly
enhanced if the part which shows field size and type is aligned. This follows
the usual style for tables in the rest of the systemd codebase.

Also, use the same style for functions: if the function signature is too long
to fit in one line, put each parameter on a separate line.

Also, for comprehension expressions, if they are split, use the usual Python
style.

Also, drop format annotations, since the code isn't automatically formatted
anymore, and automatic formatting is neither feasible nor a goal for the
systemd codebase.
2024-03-14 10:32:17 +01:00
Mike Yuan
ced3e6bc0e elf2efi: remove outdated comment mentioning linker script
Follow-up for 142f0c61a3
2023-12-10 12:50:14 +00:00
Jan Janssen
823bf39a49 elf2efi: Add GNU_RELRO support 2023-09-29 17:05:11 +02:00
Jan Janssen
898e9edc46 elf2efi: Add --copy-sections option
This makes the special PE sections available again in our output EFI
images.

Since the compiler provides no way to mark a section as not allocated,
we use GNU assembler syntax to emit the sections instead. This ensures
the section data isn't emitted twice as load segments will only contain
allocating input sections.
2023-09-29 16:56:30 +02:00
Jan Janssen
7d6fd7f099 elf2efi: Add next_section_address helper 2023-09-29 16:56:30 +02:00
Jan Janssen
5713c50d84 elf2efi: Check ELF image base if possible 2023-09-29 16:56:30 +02:00
Jan Janssen
142f0c61a3 elf2efi: Rework ELF section conversion
The main reason we need to apply a whole lot of logic to the section
conversion logic is because PE sections have to be aligned to the page
size (although, currently not even EDK2 enforces this). The process of
achieving this with a linker script is fraught with errors, they are a
pain to set up correctly and suck in general. They are also not
supported by mold, which requires us to forcibly use bfd, which also
means that linker feature detection is easily at odds as meson has a
differnt idea of what linker is in use.

Instead of forcing a manual ELF segment layout with a linker script we
just let the linker do its thing. We then simply copy/concatenate the
sections while observing proper page boundaries.
Note that we could just copy the ELF load *segments* directly and
achieve the same result. Doing this manually allows us to strip sections
we don't need at runtime like the dynamic linking information (the
elf2efi conversion is effectively the dynamic loader).

Important sections like .sbat that we emit directly from code will
currently *not* be exposed as individual PE sections as they are
contained within the ELF segments. A future commit will fix this.
2023-09-29 16:56:30 +02:00
Mike Yuan
1365355d14 elf2efi: fix a typo 2023-09-21 19:16:11 +02:00
Frantisek Sumsal
79f902eb09 Add .pylintrc to globally suppress warnings we don't really care about
Also, drop the respective disable directives from existing files.
2023-08-10 18:13:29 +02:00
Jan Janssen
ee91e06a58 elf2efi: Fix header size calculation
The PE header size calculation failed to take the PE magic and coff
header size into account, which will lead to header truncation if we are
writing only 5 sections.
2023-07-30 21:31:38 +02:00
Daan De Meyer
09444a2e76 elf2efi: Make compatible with python 3.6 again
CentOS 8 ships python 3.6 so let's try and stay compatible with that
since the only feature we're using that requires python 3.9 is the
streamlined type annotations which are trivial to convert back to
the older stuff to stay compatible with python 3.6.
2023-07-14 14:41:19 +02:00
Luca Boccassi
e18e3c4305 elf2efi: add parameter to increase reserved space for headers
When building a minimal empty addon it would not have enough
space to append sections. Add an option that will later be
used to reserve enough space.
2023-05-24 11:06:36 +01:00
Jan Janssen
ab7d54b9dd elf2efi: Do not emit an empty relocation section
At least shim will choke on an empty relocation section when loading the
binary. Note that the binary is still considered relocatable (just with
no base relocations to apply) as we do not set the
IMAGE_FILE_RELOCS_STRIPPED DLL characteristic.
2023-05-23 22:47:28 +01:00
Jan Janssen
31ffb6b183 boot: Add RISCV32 and LoongArch support
This is completely untested, but should work in theory, as it's just
adding a couple defines according to the specs.
2023-03-10 11:41:08 +01:00
Jan Janssen
2afeaf1675 boot: Bring back bootloader builds
This adds back sd-boot builds by using meson compile targets directly.
We can do this now, because userspace binaries use the special
dependency that allows us to easily separate flags, so that we don't
pass anything to EFI builds that shouldn't be passed.

Additionally, we pass a bunch of flags to hopefully disable/override any
distro provided flags that should not be used for EFI binaries.

Fixes: #12275
2023-03-10 11:41:08 +01:00