public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Richard Earnshaw <Richard.Earnshaw@foss.arm.com>
To: Sebastian Huber <sebastian.huber@embedded-brains.de>,
	binutils <binutils@sourceware.org>
Subject: Re: How to define a symbol with absolute address for AArch64?
Date: Tue, 12 Sep 2023 12:21:16 +0100	[thread overview]
Message-ID: <580f9122-f63d-628e-4503-dfcefbb1b304@foss.arm.com> (raw)
In-Reply-To: <2d247374-92a2-463b-ba23-8a6dc36d13c0@embedded-brains.de>



On 12/09/2023 11:02, Sebastian Huber wrote:
> Hello,
> 
> I would like to define a global symbol with an absolute address in an 
> assembly/C source file for the AArch64 target. This works for all other 
> architectures I tried so far, but not for AArch64:
> 
> extern char abs_symbol[];
> extern char abs_symbol_2[];
> 
> __asm__(
>    "\t.globl abs_symbol\n"
>    "\t.set abs_symbol, 0x123\n"
> );
> 
> unsigned long f_abs_symbol(void)
> {
>    return (unsigned long)abs_symbol;
> }
> 
> unsigned long f_abs_symbol_2(void)
> {
>    return (unsigned long)abs_symbol_2;
> }
> 
> unsigned long _start(void)
> {
>    return f_abs_symbol() + f_abs_symbol_2();
> }
> 
> aarch64-rtems6-gcc abs.c -Wl,--gc-sections -Wl,--defsym=abs_symbol_2=291

Have you tried -mcmodel=large?  With that I get:

0000000000400000 <f_abs_symbol>:
   400000:       90000000        adrp    x0, 400000 <f_abs_symbol>
   400004:       91004000        add     x0, x0, #0x10
   400008:       f9400000        ldr     x0, [x0]
   40000c:       d65f03c0        ret
   400010:       00000123        .word   0x00000123
   400014:       00000000        .word   0x00000000

0000000000400018 <f_abs_symbol_2>:
   400018:       90000000        adrp    x0, 400000 <f_abs_symbol>
   40001c:       9100a000        add     x0, x0, #0x28
   400020:       f9400000        ldr     x0, [x0]
   400024:       d65f03c0        ret
   400028:       00000123        .word   0x00000123
   40002c:       00000000        .word   0x00000000

0000000000400030 <_start>:
   400030:       a9be7bfd        stp     x29, x30, [sp, #-32]!
   400034:       910003fd        mov     x29, sp
   400038:       f9000bf3        str     x19, [sp, #16]
   40003c:       97fffff1        bl      400000 <f_abs_symbol>
   400040:       aa0003f3        mov     x19, x0
   400044:       97fffff5        bl      400018 <f_abs_symbol_2>
   400048:       8b000260        add     x0, x19, x0
   40004c:       f9400bf3        ldr     x19, [sp, #16]
   400050:       a8c27bfd        ldp     x29, x30, [sp], #32
   400054:       d65f03c0        ret

R.

> 
> aarch64-rtems6-objdump -d a.out
> 
> a.out:     file format elf64-littleaarch64
> 
> 
> Disassembly of section .text:
> 
> 0000000000400000 <f_abs_symbol>:
>    400000:       90000000        adrp    x0, 400000 <f_abs_symbol>
>    400004:       91048c00        add     x0, x0, #0x123
>    400008:       d65f03c0        ret
> 
> 000000000040000c <f_abs_symbol_2>:
>    40000c:       90ffe000        adrp    x0, 0 <abs_symbol-0x123>
>    400010:       91048c00        add     x0, x0, #0x123
>    400014:       d65f03c0        ret
> 
> 0000000000400018 <_start>:
>    400018:       a9be7bfd        stp     x29, x30, [sp, #-32]!
>    40001c:       910003fd        mov     x29, sp
>    400020:       f9000bf3        str     x19, [sp, #16]
>    400024:       97fffff7        bl      400000 <f_abs_symbol>
>    400028:       aa0003f3        mov     x19, x0
>    40002c:       97fffff8        bl      40000c <f_abs_symbol_2>
>    400030:       8b000260        add     x0, x19, x0
>    400034:       f9400bf3        ldr     x19, [sp, #16]
>    400038:       a8c27bfd        ldp     x29, x30, [sp], #32
>    40003c:       d65f03c0        ret
> 
> On riscv it works for example:
> 
> riscv-rtems6-gcc abs.c -Wl,--gc-sections -Wl,--defsym=abs_symbol_2=291 
> -O2 -march=rv64imafd -mabi=lp64d -mcmodel=medany
> 
> riscv-rtems6-objdump -d a.out
> 
> a.out:     file format elf64-littleriscv
> 
> 
> Disassembly of section .text:
> 
> 00000000000100b0 <f_abs_symbol>:
>     100b0:       1230051b                addiw   a0,zero,291
>     100b4:       00008067                ret
> 
> 00000000000100b8 <f_abs_symbol_2>:
>     100b8:       12300513                li      a0,291
>     100bc:       00008067                ret
> 
> 00000000000100c0 <_start>:
>     100c0:       12300793                li      a5,291
>     100c4:       1230051b                addiw   a0,zero,291
>     100c8:       00f50533                add     a0,a0,a5
>     100cc:       00008067                ret
> 

  reply	other threads:[~2023-09-12 11:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-12 10:02 Sebastian Huber
2023-09-12 11:21 ` Richard Earnshaw [this message]
2023-09-12 12:58   ` Sebastian Huber
2023-09-12 13:46     ` Xi Ruoyao
2023-09-12 14:23       ` Sebastian Huber
2023-09-12 16:18         ` Richard Earnshaw
2023-09-13  6:04           ` Sebastian Huber
2023-09-13 10:02             ` Richard Earnshaw (lists)
2023-09-14  7:30               ` Sebastian Huber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=580f9122-f63d-628e-4503-dfcefbb1b304@foss.arm.com \
    --to=richard.earnshaw@foss.arm.com \
    --cc=binutils@sourceware.org \
    --cc=sebastian.huber@embedded-brains.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).