Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relocation error in 64bit ELF object file #212

Open
TasosKL opened this issue Sep 1, 2024 · 5 comments
Open

Relocation error in 64bit ELF object file #212

TasosKL opened this issue Sep 1, 2024 · 5 comments

Comments

@TasosKL
Copy link

TasosKL commented Sep 1, 2024

Hi, all
Using uasm 2.57 to assemble this code

		.DATA

String		BYTE	"0123456789"

		.CODE

Start:		MOV	RDX, 1
		MOV	AL, String[ RDX ]
		MOV	AL, String[ 1 ][ RDX ]
		XOR	RDI, RDI
		MOV	RAX, 3Ch
		SYSCALL

		END 

results in executable code that in both cases loads AL with the same value.
Searching a bit, I found that it is a relocation problem, that is, using objdump
on the produced object file gives the following output:

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE 
0000000000000009 R_X86_64_32       String
000000000000000f R_X86_64_32       String

Assembling the same code with GNU as

		.data

String:		.ascii	"0123456789"
 
		.text

Start:		MOV	RDX, 1
		MOV	AL, String[ RDX ]
		MOV	AL, String[ 1 ][ RDX ]

		XOR	RDI, RDI
		MOV	RAX, 0x3C
		SYSCALL

		.end 

the executable code loads the correct value in AL. The output of objdump is

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE 
0000000000000009 R_X86_64_32S      .data
000000000000000f R_X86_64_32S      .data+0x0000000000000001

which shows that the second relocation entry has an addend of 1.
Can you help?

@john-terraspace
Copy link
Member

Hi,

I'm surprised that worked at all to be honest, VariableName[Rn*scale] type addressing is largely unsupported in 64bit in favour of using RIP relative forms - avoiding relocations as far as possible. I don't think this would even assemble for Windows 64bit as there is no relocation type for it.

Firstly I think UASM looks like it should be using the 32S relocation variant.
See: https://stackoverflow.com/questions/33318342/when-is-it-better-for-an-assembler-to-use-sign-extended-relocation-like-r-x86-64

@TasosKL
Copy link
Author

TasosKL commented Sep 2, 2024

Hi, John
Thank you for your reply.
The point is I cannot use RIP relative addressing as the data is in another section.
The example at SO is unrelated to my case, as the label in the example is in the code section.
Now, regarding my code and Windows 64bit, it does assemble and, after linking, produces
the correct code, ie AL is loaded with the correct value in both cases.
Anyway, I will do some more searching, and will be back.

@TasosKL
Copy link
Author

TasosKL commented Sep 5, 2024

Hi, John
The problem is definitely due to UASM.
I patched the addend field of the relocation entry with a hex editor and it works OK no matter what is
in the code section of the object file. So the problem is in the way the relocation entry is put in the object file,
it omits the addend.
Any ideas?

@john-terraspace
Copy link
Member

john-terraspace commented Sep 5, 2024 via email

@TasosKL
Copy link
Author

TasosKL commented Sep 6, 2024

Hi, John
I did the tests you suggested and the results are:

  • Setting the addend to 0 and changing the location/target offset to any value has no effect, even when I used extreme values like 7FFFFFFFh or 0FFFFFFFFh.
  • Setting the addend to a value like 1 or 2 and changing the location/target offset to any value, loads the value that corresponds to the addend, i.e. "2" for an addend of 1, "3" for 2 etc

Looking at the history file i found that there is the following change in JWASM:

03/02/2011, v2.05:

  • ELF64 format: relocations were stored in sections of type SHT_REL. Changed to SHT_RELA.

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants