Files
acrn-hypervisor/hypervisor/arch/riscv
Jian Jun Chen f410bcad42 hv: risc-v: add CFLAGS/LDFLAGS options to support relocation
The following flags are assigned to CFLAGS/LDFLAGS when trying to build
a relocatable acrn.out (ELF):
- CFLAGS += -fpie & ASFLAGS += -fpie:
  - Produces position-independent code suitable for executables (not shared libraries).
  - Uses PC-relative addressing for global variables and function calls
  - Generates code that can be loaded at any memory address
  - Together with -mcmodel=medany is used to support "Medium Position
    Independent Code Model"
- CFLAGS += -fvisibility=hidden:
  - Enabled with --gc-sections, the -fvisibility=hidden compiler flag
    increases the number of symbols the linker can identify and remove
    as unused. This reduces the final binary size and can improve
    performance
- LDFLAGS += -shared:
  - Create a shared object (ELF::e_type == 3) instead of an executable
  - This is required to support a relocatable acrn.out
- LDFLAGS += -Wl,-z,notext
  - Allow relocations in read-only segments (text relocations)
  - Resolves linking errors when absolute addresses exist in read-only
    sections
- LDFLAGS += -Wl,-Bsymbolic
  - Prioritizes local symbol definitions over external ones
  - Improves performance by avoiding PLT (Procedure Linkage Table) calls
    for local symbols
- LDFLAGS += -Wl,-z,defs
  - Forces the linker to resolve all symbol references at link time
  - Report undefined symbol errors during linking

In summary:
- -fpie + -shared: Creates a position-independent shared object that can
  be loaded as an executables
- -z,notext + -Bsymbolic: Allows internal relocations while keeping
  symbols local
- -z,defs: Ensures all symbols are properly defined, preventing runtime
  errors

We are using --start-group/--end-group to link all the modules. When
using -shared, the linker discards unused symbols by default, even if
they are marked with KEEP() in the linker script. This is because shared
libraries typically allow undefined symbols to be resolved at runtime.
To ensure the entry point is included, we need to add EXTERN(_start) to
the linker script, which forces the linker to treat _start as a required
symbol that must be resolved during the linking process.

Tracked-On: #8825
Signed-off-by: Jian Jun Chen <jian.jun.chen@intel.com>
Acked-by: Wang, Yu1 <yu1.wang@intel.com>
2025-10-22 10:15:01 +08:00
..
2025-10-22 10:15:01 +08:00
2025-10-21 14:14:55 +08:00
2025-09-29 14:01:00 +08:00
2025-09-17 08:55:12 +08:00
2025-10-16 09:45:08 +08:00