Discussion:
ld: relocation truncated to fit
(too old to reply)
Johanne Fairchild
2024-06-29 12:34:51 UTC
Permalink
Taking my first steps with

The Art of 64-bit Assembly Language, Volume 1
Randall Hyde, No Starch Press, 2022
ISBN 978-1-7185-0109-6

I am being a bit cocky in trying to use the tools: instead of learning
to use cl, I'm trying to use gcc, which I'm more used to.

%ml64 /c sort.asm
Microsoft (R) Macro Assembler (x64) Version 14.29.30138.0
Copyright (C) Microsoft Corporation. All rights reserved.

Assembling: sort.asm

So far so good. Full source code of sort.asm at the end of this post.

%cat main.c
#include <stdio.h>

void asmproc(void);

int main(void) {
printf("Invoking asmproc...\n");
asmproc();
printf("Done.\n");
}

%gcc -c main.c
%gcc -o sortme.exe sort.obj main.o
sort.obj:(.text$mn+0x75): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `sortme'
collect2.exe: error: ld returned 1 exit status

The name /sortme/ is the name of an array in sort.asm.

%file sort.obj
sort.obj: Intel amd64 COFF object file, not stripped, 4 sections, symbol offset=0x25a, 16 symbols
%file main.o
main.o: Intel amd64 COFF object file, no line number info, not stripped, 7 sections, symbol offset=0x2b4, 23 symbols

(*) Sort.asm source code

option casemap:none

nl = 10
maxlen = 256
true = 1
false = 0

bool typedef ptr byte

.const
fmt byte "sortme[%d] = %d", nl, 0

.data
sortme label dword
dword 1, 2, 16, 14
dword 3, 0, 4, 10
dword 5, 7, 15, 12
dword 8, 6, 11, 13
ssize = ($ - sortme) / sizeof dword

didswap bool ?

.code
externdef printf:proc

sort proc
push rax
push rbx
push rcx
push rdx
push r8
dec rdx

outer: mov didswap, false

xor rbx, rbx
inner: cmp rbx, rdx
jnb xinner
mov eax, [rcx + rbx*4]
cmp eax, [rcx + rbx*4 + 4]
jna dntswp

mov r8d, [rcx + rbx*4 + 4]
mov [rcx + rbx*4 + 4], eax
mov [rcx + rbx*4], r8d
mov didswap, true

dntswp: inc rbx
jmp inner

xinner: cmp didswap, true
je outer

pop r8
pop rdx
pop rcx
pop rbx
pop rax
ret
sort endp

public asmproc
asmproc proc
push rbx

sub rsp, 40

lea rcx, sortme
mov rdx, ssize
call sort

xor rbx, rbx
disp: mov r8d, sortme[rbx*4]
mov rdx, rbx
lea rcx, fmt
call printf
inc rbx
cmp rbx, ssize
jb disp

add rsp, 40
pop rbx
ret
asmproc endp
end
Johanne Fairchild
2024-07-01 19:33:47 UTC
Permalink
Post by Johanne Fairchild
Taking my first steps with
The Art of 64-bit Assembly Language, Volume 1
Randall Hyde, No Starch Press, 2022
ISBN 978-1-7185-0109-6
I am being a bit cocky in trying to use the tools: instead of learning
to use cl, I'm trying to use gcc, which I'm more used to.
For the record, I did manage to learn enough of CL to compile and the
link the program. It was instructive to clear up what the issue is
about as I'll show below. I'm still unable to compile and the link the
program using GCC, so I'm still interested in learning how to do that.
(If that makes sense. If you tell me that these a MASM object code
should never be linked by the GNU linker, I'm all ears.)

Here's what I did.

%ml64 /c /Zi /Cp sort.asm
Microsoft (R) Macro Assembler (x64) Version 14.29.30148.0
Copyright (C) Microsoft Corporation. All rights reserved.

Assembling: sort.asm

Object code produced by MASM.

%cl /O2 /Zi /utf-8 /EHa /Fesort.exe main.c sort.obj
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30148 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

main.c
Microsoft (R) Incremental Linker Version 14.29.30148.0
Copyright (C) Microsoft Corporation. All rights reserved.

/debug
/out:sort.exe
main.obj
sort.obj
sort.obj : error LNK2017: 'ADDR32' relocation to 'sortme' invalid without /LARGEADDRESSAWARE:NO
LINK : fatal error LNK1165: link failed because of fixup errors

So it seems I'm being bitten by the fact that my program is using an
instruction that's a variant of the indirect-plus-offset addressing mode
that's not available when my program wants to access the entirety of
memory of a 64-bit machine. Since I'm not acessing much memory, there's
no need for this, so I can follow the suggestion of the linker.

C:\sys\emacs\usr\tmp>cl /O2 /Zi /utf-8 /EHa /Fesort.exe main.c sort.obj /link /LARGEADDRESSAWARE:NO
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30148 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

main.c
Microsoft (R) Incremental Linker Version 14.29.30148.0
Copyright (C) Microsoft Corporation. All rights reserved.

/debug
/out:sort.exe
/LARGEADDRESSAWARE:NO
main.obj
sort.obj

So that worked. So my question becomes---how can I tell the GNU linker
to assemble my program ignoring the fact that I cannot reach the whole
of the machine's memory? In other words, what is the GNU linker's

/largeaddressaware:no

option, assuming there is one? Thank you so much.

(*) The program

It's a bubble sort implementation.

%./sort.exe
Invoking asmproc...
sortme[0] = 0
sortme[1] = 1
sortme[2] = 2
sortme[3] = 3
sortme[4] = 4
sortme[5] = 5
sortme[6] = 6
sortme[7] = 7
sortme[8] = 8
sortme[9] = 10
sortme[10] = 11
sortme[11] = 12
sortme[12] = 13
sortme[13] = 14
sortme[14] = 15
sortme[15] = 16
Done.
wolfgang kern
2024-07-03 13:38:33 UTC
Permalink
Post by Johanne Fairchild
Post by Johanne Fairchild
Taking my first steps with
The Art of 64-bit Assembly Language, Volume 1
Randall Hyde, No Starch Press, 2022
ISBN 978-1-7185-0109-6
If you like to do ASM then don't waste time/money on RH products.
NASM, FASM and even MASM are the much lesser detouring options
Post by Johanne Fairchild
Post by Johanne Fairchild
I am being a bit cocky in trying to use the tools: instead of learning
to use cl, I'm trying to use gcc, which I'm more used to.
:) are you a relative of Laura Fairchild ?

...
Post by Johanne Fairchild
So that worked. So my question becomes---how can I tell the GNU linker
to assemble my program ignoring the fact that I cannot reach the whole
of the machine's memory? In other words, what is the GNU linker's
/largeaddressaware:no
option, assuming there is one? Thank you so much.
Sorry, I never used GNU so can't help here.
__
wolfgang
Johanne Fairchild
2024-07-04 22:02:44 UTC
Permalink
Post by wolfgang kern
Post by Johanne Fairchild
Post by Johanne Fairchild
Taking my first steps with
The Art of 64-bit Assembly Language, Volume 1
Randall Hyde, No Starch Press, 2022
ISBN 978-1-7185-0109-6
If you like to do ASM then don't waste time/money on RH products.
Can you elaborate on this?
Post by wolfgang kern
NASM, FASM and even MASM are the much lesser detouring options
What do you mean by this? It seems as though you're saying---don't read
a book. I don't know what you're saying. What do you mean by ``lesser
detouring options''? It makes me feel that you're saying Randall Hyde
takes the reader through a convoluted path.
Post by wolfgang kern
Post by Johanne Fairchild
Post by Johanne Fairchild
I am being a bit cocky in trying to use the tools: instead of learning
to use cl, I'm trying to use gcc, which I'm more used to.
:) are you a relative of Laura Fairchild ?
I'm not. Who is she?
Post by wolfgang kern
...
Post by Johanne Fairchild
So that worked. So my question becomes---how can I tell the GNU linker
to assemble my program ignoring the fact that I cannot reach the whole
of the machine's memory? In other words, what is the GNU linker's
/largeaddressaware:no
option, assuming there is one? Thank you so much.
Sorry, I never used GNU so can't help here.
Thanks in any case.
wolfgang kern
2024-07-05 09:36:34 UTC
Permalink
Post by Johanne Fairchild
Post by wolfgang kern
Post by Johanne Fairchild
Taking my first steps with
The Art of 64-bit Assembly Language, Volume 1
Randall Hyde, No Starch Press, 2022
ISBN 978-1-7185-0109-6
If you like to do ASM then don't waste time/money on RH products.
Can you elaborate on this?
Post by wolfgang kern
NASM, FASM and even MASM are the much lesser detouring options
What do you mean by this? It seems as though you're saying---don't read
a book. I don't know what you're saying.
nothing wrong with books :) except when they are misleading.
the internet is full of reliable sources to learn x64 opcodes.
Post by Johanne Fairchild
What do you mean by ``lesser detouring options''?
You can try both ways to figure it out yourself,
but you might save "some" time by avoiding RH.
Post by Johanne Fairchild
It makes me feel that you're saying Randall Hyde
takes the reader through a convoluted path.
yes. I have some issues with Randy since decades [red flag warning].
it has been proven in this group that his HLA and following stuff are
not at all ASM. his products are just text parsing detours with his own
weird language.
Post by Johanne Fairchild
Post by wolfgang kern
Post by Johanne Fairchild
I am being a bit cocky in trying to use the tools: instead of learning
to use cl, I'm trying to use gcc, which I'm more used to.
:) are you a relative of Laura Fairchild ?
I'm not. Who is she?
a famous well skilled ASM programmer who posted lots of good ideas in
the past. And we saw much more female programmers around 2000 here.
__
wolfgang
Johanne Fairchild
2024-07-05 12:20:21 UTC
Permalink
Post by wolfgang kern
Post by Johanne Fairchild
Post by wolfgang kern
Post by Johanne Fairchild
Taking my first steps with
The Art of 64-bit Assembly Language, Volume 1
Randall Hyde, No Starch Press, 2022
ISBN 978-1-7185-0109-6
If you like to do ASM then don't waste time/money on RH products.
Can you elaborate on this?
Post by wolfgang kern
NASM, FASM and even MASM are the much lesser detouring options
What do you mean by this? It seems as though you're saying---don't read
a book. I don't know what you're saying.
nothing wrong with books :) except when they are misleading.
the internet is full of reliable sources to learn x64 opcodes.
Hm, but assembly is more than opcodes. In the book above, at least up
to the chapter 5, we're just learning about an x86-64 machine works.

Is there a book you recommend?
Post by wolfgang kern
Post by Johanne Fairchild
It makes me feel that you're saying Randall Hyde
takes the reader through a convoluted path.
yes. I have some issues with Randy since decades [red flag warning].
it has been proven in this group that his HLA and following stuff are
not at all ASM. his products are just text parsing detours with his
own weird language.
By ASM you mean ``assembly'', right?
Post by wolfgang kern
Post by Johanne Fairchild
Post by wolfgang kern
Post by Johanne Fairchild
I am being a bit cocky in trying to use the tools: instead of learning
to use cl, I'm trying to use gcc, which I'm more used to.
:) are you a relative of Laura Fairchild ?
I'm not. Who is she?
a famous well skilled ASM programmer who posted lots of good ideas in
the past. And we saw much more female programmers around 2000 here.
Cool!
wolfgang kern
2024-07-05 13:22:40 UTC
Permalink
On 05/07/2024 14:20, Johanne Fairchild wrote:
...
Post by Johanne Fairchild
Hm, but assembly is more than opcodes. In the book above, at least up
to the chapter 5, we're just learning about an x86-64 machine works.
Is there a book you recommend?
the matter may be too complex to fit into one book

I learned everything on AMD64 aka 8664 from the CPU designers.
AMD supported me with downloadable PDFs for free.
get all five volumes to see every aspect of the CPU.
INTEL has it as well and both sources look almost identical :)
even Intel put it all into three volumes.

I created my own CPU book by printing only the PDF parts I were most
interested in and added lots of handwritten notes.
It's now on my shelf spanning three thick folders (~900 pages).

one more good source is sandpile.org

sorry for my links to both vendors seem to be outdated,
no wonder because I started it more than two decades ago.

...
Post by Johanne Fairchild
By ASM you mean ``assembly'', right?
yeah :) in opposition to HLL (High Level Language)

I myself work even below ASM: pure metal code in hexadecimal.
and I hate all these abstracted detouring stuff,
so I never tried to learn C and similar HLL.
...
[Laura Fairchild]
Post by Johanne Fairchild
Post by wolfgang kern
Post by Johanne Fairchild
I'm not. Who is she?
a famous well skilled ASM programmer who posted lots of good ideas in
the past. And we saw much more female programmers around 2000 here.
Cool!
Google Archive still contains many posts from that glorious times.
__
wolfgang
Kerr-Mudd, John
2024-07-05 15:53:27 UTC
Permalink
On Fri, 5 Jul 2024 15:22:40 +0200
Post by wolfgang kern
...
[bad carat count, but hey]
Post by wolfgang kern
...
[Laura Fairchild]
Post by wolfgang kern
Post by Johanne Fairchild
I'm not. Who is she?
a famous well skilled ASM programmer who posted lots of good ideas in
the past. And we saw much more female programmers around 2000 here.
Cool!
Google Archive still contains many posts from that glorious times.
"CM3" was/is a wonderful bit of small executable text.
--
Bah, and indeed Humbug.
Loading...