Relocation truncated to fit error from ld

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Relocation truncated to fit error from ld

Post by bzt »

There's actually a section devoted to this on the wiki: https://wiki.osdev.org/X86-64#Text_Segment_Types.

I wouldn't recommend mcmodel=kernel, as that's for Linux hacks specifically. Might change anytime without any notification...

Code: Select all

-mcmodel=kernel

    Generate code for the kernel code model. The kernel runs in the negative 2 GB of the address space. This model has to be used for Linux kernel code.
Cheers,
bzt
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Relocation truncated to fit error from ld

Post by Octocontrabass »

sj95126 wrote:Um, ok, but I was asking about gas, not gcc. I know gcc has -mcmodel.
I am talking about GAS. There is no equivalent option for GAS because the option changes which instructions are emitted, not how the instructions are encoded.
sj95126 wrote:And yes, some 64-bit instructions can only take a 32-bit operand, but some, like lea, can take a 64-bit operand, yet gas is forcing this to be truncated to 32-bits. You can even see from the encoding:
The effective address is 64 bits, but the offset portion can only be 32 bits. This is a limitation of the x64 architecture, not a limitation of GAS. Most instructions cannot encode an offset with more than 32 bits.
bzt wrote:I wouldn't recommend mcmodel=kernel, as that's for Linux hacks specifically.
Where does the documentation say that it's a Linux-specific hack? It works for any x64 code meant to be linked in the highest 2GiB of address space; the Linux kernel just happens to be the most popular example.
sj95126
Member
Member
Posts: 151
Joined: Tue Aug 11, 2020 12:14 pm

Re: Relocation truncated to fit error from ld

Post by sj95126 »

Octocontrabass wrote:
bzt wrote:I wouldn't recommend mcmodel=kernel, as that's for Linux hacks specifically.
Where does the documentation say that it's a Linux-specific hack? It works for any x64 code meant to be linked in the highest 2GiB of address space; the Linux kernel just happens to be the most popular example.
It's in the OSDev page for X86_64:

"From experience, you should not use "-mcmodel=kernel". That is a hack for the Linux kernel on x86_64."

Whether or not that's still reasonable advice, that's where people are getting it.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Relocation truncated to fit error from ld

Post by kzinti »

This is non-sense. It's nothing Linux specific. It is x86_64 specific and perfectly usable. There is nothing hacky about it. It is not going to "change any time".

In fact it has been recommended many times in the past on these forums and the wiki to use "-mcmodel=kernel" for x86_64, and for good reasons. Putting the kernel at the top of memory is good practice to cleanly separate user and kernel space.

This was added to that wiki page by bzt, so he is just repeating the same thing.
sj95126
Member
Member
Posts: 151
Joined: Tue Aug 11, 2020 12:14 pm

Re: Relocation truncated to fit error from ld

Post by sj95126 »

Octocontrabass wrote:The effective address is 64 bits, but the offset portion can only be 32 bits. This is a limitation of the x64 architecture, not a limitation of GAS. Most instructions cannot encode an offset with more than 32 bits.
Right, ok, duh. Brain fade. Been stuck at home too long. That's why the alternate form movabs, to tell the assembler you want to use the full 64-bit address not 32-bit offset -> 64-bit effective. mov is the only instruction that has a moffs64 operand.
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: Relocation truncated to fit error from ld

Post by nullplan »

bzt wrote:I wouldn't recommend mcmodel=kernel, as that's for Linux hacks specifically. Might change anytime without any notification...
That is FUD! Code models are defined in the ABI, and for one thing, ABI will only change over Linus's dead body, and for two, they are defined without regard for the OS. Code model small (the default) means that all link time addresses are below 2GB. Code model medium means that most link time addresses are below 2GB, but some references to special sections exist that might exceed that limit. Code model large means that any link time address can exceed 2GB. And code model kernel means that all link time addresses are above -2GB. Nothing Linux specific about this, at all. It is just that Linux uses this model, which is documented in the place you indicated.

What was this about a Wiki page? Oh... this one: https://wiki.osdev.org/X86-64#Text_Segment_Types
It worth noting that code models are different for architectures,
On a page titled "X86-64", we find architecture-specific information? Surely not!
That is a hack for the Linux kernel on x86_64. It is not available for other architectures,
Again, yes of course the information is specific to x86-64. It is on a page titled "X86-64".
It is better if you use a proper linker script instead to place your kernel into the negative address range.
You have not understood the issue. The code model switch does not replace a linker script. You still need one. The code model switch makes the compiler output different assembler code, so the assembler will emit different relocation types. Which will then allow your code to be linked at a high address, but you still need a linker script to do that (well, or you use -Ttext, but then you miss out on the other advantages of having a linker script).

Since there is objectively no value to those two sentences, I have removed them.
Carpe diem!
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Relocation truncated to fit error from ld

Post by iansjack »

bzt wrote:There's actually a section devoted to this on the wiki: https://wiki.osdev.org/X86-64#Text_Segment_Types.
Referring to a piece that you yourself wrote is possibly not the most convincing way to reinforce an argument. How about an independent reference on the subject?
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Relocation truncated to fit error from ld

Post by bzt »

sj95126 wrote:Um, ok, but I was asking about gas, not gcc. I know gcc has -mcmodel.
From that wiki page: For Assembly, you must use the "movabs" instruction instead of "mov".

But guys, what are you doing? Don't make fools of yourselves! Seriously...
iansjack wrote:How about an independent reference on the subject?
How about reading the GNU manual I've linked? I've even quoted the relevant part from the GNU manual!!!
nullplan wrote:That is FUD! Code models are defined in the ABI,
Wrong! Code models are based on the instruction encoding and addressing scheme! The mcmodel=small and mcmodel=large uses the same registers to pass arguments to functions (as specified by the ABI)! There are common parts true, and the spec overlaps, sure but they are two things! Why does one specify them as two separate gcc command line options, what do you think?
nullplan wrote:On a page titled "X86-64", we find architecture-specific information? Surely not!
What's your problem with a warning and an example? You most certainly need it, because you don't understand the concept, and you did not know that the same mcmodel covers different memory ranges on different architectures. It's not that AArch64 instruction coding is explained in great details on that page! It just says "It worth noting" that there's a difference. It is also important to mention if a particular gcc option is x86_64 only, and therefore non-portable!
kzinti wrote:This is non-sense. It's nothing Linux specific.
From the GNU gcc manual: "This model has to be used for Linux kernel code." Which means if and when the Linux kernel code mapping changes, this model will change accordingly without a doubt.

Cheers,
bzt
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Relocation truncated to fit error from ld

Post by nexos »

From the docs
GCC documentation wrote:-mcmodel=kernel
Generate code for the kernel code model. The kernel runs in the negative 2 GB of the address space. This model has to be used for Linux kernel code.
It doesn't say it was made for Linux. Linux is just a good example. It is in other compilers like the Intel C compiler. I doubt it was made solely for Linux. My old kernel used -fpic. I didn't ever have issues.
As for sj95126's question, I think GAS is overly complicated. When using NASM, you use the same MOV instruction no matter what. To me AT&T syntax seems to be more for other archs. x86 code in GAS feels kludgy to me. So I use NASM.
Also,
OSDev Wiki (by bzt) wrote:It is not available for other architectures, and it might change any time to suit the Linux kernel needs.
How often does a kernel change base addresses? Linux is staying at 0xFFFFFFFF80000000 (or wherever it is linked to).
Also, nullplan's change on the wiki is best, as a wiki is not a place for opinions. Just put the facts, and questions can be asked here.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: Relocation truncated to fit error from ld

Post by nullplan »

bzt wrote:But guys, what are you doing? Don't make fools of yourselves! Seriously...
Look in the mirror at some point.
bzt wrote:Wrong! Code models are based on the instruction encoding and addressing scheme!
Non sequitur. Anyway, the specific terms with the specific meanings are defined in the ABI. Specifically this document, p. 33f. That document is finalized, any change about it, especially about something as major as the code models, would be a breaking change. And it would not break some backwater architecture only five people in the world are using, it would break the main architecture new Linux installations are deployed on. This would only happen over Linus's dead body, and rightly so.

Or did you think the GCC guys pulled those code models out of their unmentionables?
bzt wrote:What's your problem with a warning and an example?
An irrelevant example (that page is about x86_64, bringing up AArch64 helps nobody), and a warning that is wrong. Wrong information deserves to be expunged. The warning "this may change at any time" in particular just creates fear, uncertainty, and doubt in the stability of the psABI. Which would be fine if it were justified, but it isn't. First came the ABI, then came the Linux implementation of it.
bzt wrote: It is also important to mention if a particular gcc option is x86_64 only, and therefore non-portable!
Portable? What? We are talking about architecture-specific things, which is why they are on the architecture page. Of course the code models are not portable, but they never are, anyway. (Virtual) memory layout is something that has to be fitted to each architecture separately. I don't think anyone misunderstands that. The option shows up in the GCC manual under "x86 Options", in the section on "Machine-Dependent Options". I don't know how anyone would, in any way, get the impression that those options are portable to other architectures.
bzt wrote:From the GNU gcc manual: "This model has to be used for Linux kernel code." Which means if and when the Linux kernel code mapping changes, this model will change accordingly without a doubt.
You misunderstand that sentence: Just because Linux is using the kernel code model doesn't mean the model exists only for Linux. The model exists, because as a side effect of how x86 instruction encodings work, you can more easily access memory within 2GB from the current address, and placing everything within 2GB from zero ensures this. This encoding has been the same since the mid-80s (since the 386), and nobody is going to change it anytime soon.

Or put another way: The sentence "The color red must be used on all warning signs" does not mean that warning signs are all the color red is good for, right?
Carpe diem!
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Relocation truncated to fit error from ld

Post by kzinti »

bzt wrote:
kzinti wrote:This is non-sense. It's nothing Linux specific.
From the GNU gcc manual: "This model has to be used for Linux kernel code." Which means if and when the Linux kernel code mapping changes, this model will change accordingly without a doubt.
It absolutely does not mean what you say it means. Your conclusion has no logical connection and is not a consequence of the fact that Linux uses the kernel memory model.
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Relocation truncated to fit error from ld

Post by PeterX »

kzinti wrote:
bzt wrote:
kzinti wrote:This is non-sense. It's nothing Linux specific.
From the GNU gcc manual: "This model has to be used for Linux kernel code." Which means if and when the Linux kernel code mapping changes, this model will change accordingly without a doubt.
It absolutely does not mean what you say it means.
Sorry to intervene here, but I, too, think it will not change if Linux changes. It's a memory model.

Greetings
Peter
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Relocation truncated to fit error from ld

Post by nexos »

bzt and nullplan should carry this on in the x86_64 talk page or in PM, not on the forum.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Relocation truncated to fit error from ld

Post by bzt »

I do not wish to carry this any further. I've felt the need to warn you, that's all. I've told you that mcmodel and ABI are not the same (you can use Ada, Pascal, SysV and fastcall ABIs too with different memory models), and that mcmodel=kernel is non-portable.

If you don't care about the warning, that's your problem, not mine. You can mess up your OS and build environment as much as you want :-)

Have a nice day!
bzt
Post Reply