Skip to content

Instantly share code, notes, and snippets.

@mbolivar
Last active December 6, 2018 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbolivar/e2704c48e92f13a0a731198603093dbc to your computer and use it in GitHub Desktop.
Save mbolivar/e2704c48e92f13a0a731198603093dbc to your computer and use it in GitHub Desktop.
--------------------
$ cat reloc.c
long global_symbol;
int _start(void) { return global_symbol; }
--------------------
$ cat reloc-weak.c
__attribute__((weak)) long global_symbol;
int _start(void) { return global_symbol; }
--------------------
# For these to work, you need riscv.ld to exist
# in the same directory. Get it from riscv-unknown-elf-ld --verbose.
# Note the RV32M1 toolchain is in use.
# Prebuilts:
# https://github.com/open-isa-org/open-isa.org/releases
$ riscv32-unknown-elf-gcc reloc.c -o reloc -nostdlib -nostartfiles --save-temps -Os
$ riscv32-unknown-elf-gcc reloc-weak.c -o reloc-weak -nostdlib -nostartfiles --save-temps -Os
--------------------
$ riscv32-unknown-elf-objdump -dtr reloc.o
[...]
SYMBOL TABLE:
[...]
00000004 O *COM* 00000004 global_symbol
[...]
00000000 <_start>:
0: 000007b7 lui a5,0x0
0: R_RISCV_HI20 global_symbol
0: R_RISCV_RELAX *ABS*
4: 0007a503 lw a0,0(a5) # 0 <_start>
4: R_RISCV_LO12_I global_symbol
4: R_RISCV_RELAX *ABS*
8: 8082 ret
$ riscv32-unknown-elf-objdump -dtr reloc-weak.o
[...]
SYMBOL TABLE:
[...]
00000000 w O .sbss 00000004 global_symbol
[...]
00000000 <_start>:
0: 000007b7 lui a5,0x0
0: R_RISCV_HI20 global_symbol
0: R_RISCV_RELAX *ABS*
4: 0007a503 lw a0,0(a5) # 0 <_start>
4: R_RISCV_LO12_I global_symbol
4: R_RISCV_RELAX *ABS*
8: 8082 ret
--------------------
$ riscv32-unknown-elf-objdump -dtr reloc
[...]
SYMBOL TABLE:
[...]
0001107c g O .bss 00000004 global_symbol
[...]
00010074 <_start>:
10074: 67c5 lui a5,0x11
10076: 07c7a503 lw a0,124(a5) # 1107c <global_symbol>
1007a: 8082 ret
$ riscv32-unknown-elf-objdump -dtr reloc-weak
[...]
SYMBOL TABLE:
[...]
00011080 w O .sbss 00000004 global_symbol
[...]
00010074 <_start>:
10074: 000117b7 lui a5,0x11
10078: 0807a503 lw a0,128(a5) # 11080 <global_symbol>
1007c: 8082 ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment