Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nanoMIPS][LLD] nanoMIPS target added to lld #51

Conversation

AndrijaSyrmia
Copy link
Collaborator

@AndrijaSyrmia AndrijaSyrmia commented Sep 6, 2024

Support for nanoMIPS little endian 32bit target added to lld

@AndrijaSyrmia AndrijaSyrmia force-pushed the andrija/nanomips-llvm16/lld branch from 85a3df7 to e1619d4 Compare September 11, 2024 09:17
@AndrijaSyrmia AndrijaSyrmia marked this pull request as ready for review September 11, 2024 09:18
Copy link
Collaborator

@djtodoro djtodoro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments inline

@AndrijaSyrmia AndrijaSyrmia force-pushed the andrija/nanomips-llvm16/lld branch from e1619d4 to 02081f8 Compare September 13, 2024 12:09
@AndrijaSyrmia AndrijaSyrmia force-pushed the andrija/nanomips-llvm16/lld branch from fa6c8aa to 446214d Compare September 27, 2024 06:18
@AndrijaSyrmia AndrijaSyrmia force-pushed the andrija/nanomips-llvm16/lld branch 2 times, most recently from 215483b to 63f8f51 Compare October 11, 2024 11:51
@AndrijaSyrmia AndrijaSyrmia force-pushed the andrija/nanomips-llvm16/lld branch from 63f8f51 to 4099547 Compare October 30, 2024 13:39
@AndrijaSyrmia
Copy link
Collaborator Author

@jchang-github Hey John, do you maybe have any more comments/suggestions on lld? I've added some more tests as well to test the coverage later.

There's also work left in reformatting the code.

@AndrijaSyrmia AndrijaSyrmia force-pushed the andrija/nanomips-llvm16/lld branch 2 times, most recently from aaa04e9 to 55e0959 Compare November 19, 2024 11:55
@AndrijaSyrmia
Copy link
Collaborator Author

Code tidied, and did the code coverage. 80% of newly added lines are covered which is expected (there are some missed opportunities, errors and unreachables are not covered, and still not implemented parts are waiting for implementation to be tested).

@AndrijaSyrmia AndrijaSyrmia force-pushed the andrija/nanomips-llvm16/lld branch from 55e0959 to c7cc2bd Compare November 20, 2024 17:28
AndrijaSyrmia and others added 7 commits November 27, 2024 10:13
Features added:
- nanoMIPS relocation resolving
- nanoMIPS eflags calculation
- .nanoMIPS.abiflags section creation
- nanoMIPS _gp symbol setup
- special .eh_frame handling in maybeReportUndefined
- R_NANOMIPS_PCHI20 renamed to R_NANOMIPS_PC_HI20
Added nanoMIPS specific linker relaxations/expansions:
- Transformations are conducted through a driver class
  NanoMipsTransformController. It scans through relocs of executable
  input sections and performs relaxations/expansions if possible or
  necessary
- New nanoMIPS specific options used in transformations added to lld
- New TableGen backend (NanoMipsTransformationPropertyEmitter.cpp) for
  emitting properties of instructions and relocations, transformation
  templates used in linker transformations
Changed parsing of the linker script, as some nanoMIPS linker scripts
require different set of rules for parsing:
- Dot operator can be used as section address expression, and its
  value should be determined by the latest value of dot, not the
  current position in the memory region
- Alignment for output section is not ignored for nanoMIPS if output
  section has got address expression
- SHT_NOBITS sections don't occupy load addresses
- OVERLAY sections can have a load region and memory region and
  assignment or byte commands in them.

This way of parsing is enabled if the option
--nanomips-custom-linker-script-type is set, which is off by default.
Changed the way .eh_frame parsing is done for nanoMIPS, as they may
contain relocations referring to the size of the CIEs/FDEs. lld reads
.eh_frame sections using these sizes so it is necessary that the size
value is available at the time of .eh_frame section parsing.

It seems like integrated assembler for nanoMIPS is not resolving this
relocation at assembly time, but the gnu's assembler for nanoMIPS is
resolving it.
Linker relaxation may change relocations (offsets and types). However,
when --emit-relocs is used, relocations are simply copied from the input
section causing a mismatch with the corresponding (relaxed) code
section.

This patch fixes this as follows: for non-relocatable RISC-V binaries,
`InputSection::copyRelocations` reads relocations from the relocated
section's `relocations` array (since this gets updated by the relaxation
code). For all other cases, relocations are read from the input section
directly as before.

In order to reuse as much code as possible, and to keep the diff small,
the original `InputSection::copyRelocations` is changed to accept the
relocations as a range of `Relocation` objects. This means that, in the
general case when reading from the input section, raw relocations need
to be converted to `Relocation`s first, which introduces quite a bit of
boiler plate. It also means there's a slight code size increase due to
the extra instantiations of `copyRelocations` (for both range types).

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D159082
Similar to RISC-V relaxations, nanoMIPS transformations change
relocations and they need to be updated before writing them down to
output. One difference is that nanoMIPS can add new relocations, not
just change them like RISC-V, so during linker transformations size of
reloc sections need to be updated if --emit-relocs option is on.
eh_frame start and end symbols need to be defined if there is an
eh_frame section. These symbols are needed by libunwind when used
without libgcc.
In some linker scripts intended to be used for nanoMIPS builds, there
are OUTPUT_FORMAT directives specifying elf32-tradlittlemips or
elf32-tradbigmips as output formats. These bfd names result in lld
choosing MIPS to be the target machine instead of nanoMIPS. This patch
checks if that is the case and changes the machine to be nanoMIPS.
The STARTUP linker script directive works similar to the INPUT directive
with one file specified, but it also puts the specified file as the
first file to be parsed, similar to putting an object file to be the
first parameter on the command line.

Errors are emitted if two STARTUP directives were found (there can't be
two or more first files) and if archive files are passed as the
argument (not yet supported).
@AndrijaSyrmia AndrijaSyrmia force-pushed the andrija/nanomips-llvm16/lld branch from 6d8daec to 31ffb42 Compare November 27, 2024 12:47
@AndrijaSyrmia
Copy link
Collaborator Author

Just rebased to current nanomips-llvm16 in previous force push

@AndrijaSyrmia AndrijaSyrmia merged commit b5edd2b into MediaTek-Labs:nanomips-llvm16 Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants