Skip to content

Commit

Permalink
Add "ax" flags to .section directives in assembly
Browse files Browse the repository at this point in the history
This commit fixes a linking error when using LLVM's lld.

llvm-as doesn't assume or assign section flags for input section if
not specified. This will make the section non-allocatable and put
into a none segment and trigger relocation linking errors against
symbols in non-allocatble sections.

For example, when building with LLVM/lld for AArch64, the following
error occurs:
(traps.S.obj:(function arm_vector_table: .vectors+0x0): has non-ABS
relocation R_AARCH64_JUMP26 against symbol 'invalid_vector_entry')
This does not happen with ld.bfd (GNU's linker) as it seems to allow
relocations against symbols in non-allocatable sections.

The GNU's assembler documentation states that:
"If no flags are specified, the default flags depend upon the
section name. If the section name is not recognized, the default
will be for the section to have none of the above flags: it will
not be allocated in memory, nor writable, nor executable.
The section will contain data. [1]"

This commit explicitly sets .section flags in assembly to avoid
this error and for better intentionality (and good practice)
without relying on toolchains handling flags and linkage differently.

[1] https://sourceware.org/binutils/docs/as/Section.html

Sponsored by: DARPA.

Signed-off-by: Hesham Almatary <hesham.almatary@cl.cam.ac.uk>
  • Loading branch information
heshamelmatary authored and lsf37 committed Oct 30, 2023
1 parent b9b36e5 commit 5b4fb3e
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/arch/arm/64/head.S
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#define SCTLR sctlr_el1
#endif

.section .boot.text
.section .boot.text, "ax"
BEGIN_FUNC(_start)
/* Save x4 and x5 so we don't clobber it */
mov x7, x4
Expand Down
4 changes: 2 additions & 2 deletions src/arch/arm/64/traps.S
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
b \label
.endm

.section .vectors
.section .vectors, "ax"

BEGIN_FUNC(arm_vector_table)
ventry invalid_vector_entry // Synchronous EL1t/EL2t
Expand All @@ -67,7 +67,7 @@ BEGIN_FUNC(arm_vector_table)
ventry invalid_vector_entry // SError 32-bit EL0/EL1
END_FUNC(arm_vector_table)

.section .vectors.text
.section .vectors.text, "ax"

.macro kernel_enter
/* Storing thread's stack frame */
Expand Down
2 changes: 1 addition & 1 deletion src/arch/riscv/traps.S
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#define REGBYTES (CONFIG_WORD_SIZE / 8)

.section .text
.section .text, "ax"

.global trap_entry
.extern c_handle_syscall
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86/32/head.S
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <config.h>
#include <machine/assembler.h>

.section .phys.text
.section .phys.text, "ax"

BEGIN_FUNC(enable_paging)
# Set PSE (bit 4) to enable 4M pages
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86/32/machine_asm.S
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <machine/assembler.h>

.section .boot.text
.section .boot.text, "ax"

/* user-level selectors from </arch/mode/object/structures.h> */
#define GDT_NULL 0
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86/32/traps.S
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ int_##number: \
SET_SELECTOR(fs, SEL_FS, tmp) \
SET_SELECTOR(gs, SEL_GS, tmp)

.section .text
.section .text, "ax"

INT_HANDLER_WITHOUT_ERR_CODE(00)
INT_HANDLER_WITHOUT_ERR_CODE(01)
Expand Down
4 changes: 2 additions & 2 deletions src/arch/x86/64/machine_asm.S
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* as integer type according to the ABI.
*/

.section .boot.text
.section .boot.text, "ax"

.section .text
.section .text, "ax"

BEGIN_FUNC(out8) # port, value
movq %rdi, %rdx
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86/64/traps.S
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ int_##number: \
#define INT_HANDLER_WITH_ERR_CODE(number,except) INT_HANDLER_COMMON(number,,except)
#define INT_HANDLER_WITHOUT_ERR_CODE(number,except) INT_HANDLER_COMMON(number,pushq $0x0,except)

.section .text
.section .text, "ax"
.code64

INT_HANDLER_WITHOUT_ERR_CODE(00,1)
Expand Down

0 comments on commit 5b4fb3e

Please sign in to comment.