public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
To: Xi Ruoyao <xry111@xry111.site>,
	Richard Earnshaw <Richard.Earnshaw@foss.arm.com>,
	binutils <binutils@sourceware.org>
Subject: Re: How to define a symbol with absolute address for AArch64?
Date: Tue, 12 Sep 2023 16:23:32 +0200	[thread overview]
Message-ID: <2706d31f-7f35-46f9-9a20-78cf069e9602@embedded-brains.de> (raw)
In-Reply-To: <ffe325eaa51dedb1966bcc8ab8b84a640545b610.camel@xry111.site>

On 12.09.23 15:46, Xi Ruoyao wrote:
> On Tue, 2023-09-12 at 14:58 +0200, Sebastian Huber wrote:
>> On 12.09.23 13:21, Richard Earnshaw wrote:
>>>
>>> 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:
>> I get the same result with -mcmodel=large. With -mcmodel=tiny I get:
>>
>> aarch64-rtems6-gcc abs.c -Wl,--gc-sections -Wl,--defsym=abs_symbol_2=291
>> -mcmodel=tiny
>> /tmp/ccKUnvyq.o: in function `f_abs_symbol_2':
>> abs.c:(.text+0x8): relocation truncated to fit: R_AARCH64_ADR_PREL_LO21
>> against symbol `abs_symbol_2' defined in*ABS*  section in a.out
>> collect2: error: ld returned 1 exit status
>>
>> Is this a tool bug?
> No, it's how code models are defined.  GCC documentation says clearly:
> 
>         -mcmodel=tiny
>             Generate code for  the  tiny  code  model.   The  program  and  its
>             statically  defined  symbols  must  be  within  1MB  of each other.
>             Programs can be statically or dynamically linked.
> 
> Here the text is located at 0x400000 but abs_symbol_2 is at 0x123, thus
> violating the definition of -mcmodel=tiny.

Yes, this makes sense.

> 
>> Is there some way to make this working with -mcmodel=small?
> No because -mcmodel=small only assumes the program and the symbols are
> within*a*  4GB range - for example it's allowed to be [47GB, 51GB).
> This is different from the default code model of RISC-V (-
> mcmodel=medlow) where the symbols must be in [-2GB, 2GB).
> 
> If you really think GCC should support this you can ask GCC for adding a
> new code model.  Anyway this is not a linker issue because the adrp-add
> pairs are generated by GCC.

Thanks for the explanation.

I guess what reliably works across code models is using the address of 
an existing symbol and then add a constant which fits into the code 
model. To retrieve the constant, we just have to subtract the address of 
the other symbol.

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

  reply	other threads:[~2023-09-12 14:23 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
2023-09-12 12:58   ` Sebastian Huber
2023-09-12 13:46     ` Xi Ruoyao
2023-09-12 14:23       ` Sebastian Huber [this message]
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=2706d31f-7f35-46f9-9a20-78cf069e9602@embedded-brains.de \
    --to=sebastian.huber@embedded-brains.de \
    --cc=Richard.Earnshaw@foss.arm.com \
    --cc=binutils@sourceware.org \
    --cc=xry111@xry111.site \
    /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).