rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

ld linking error

Thu Jun 22, 2017 5:18 am

Hi all, I tried to run some ld commands with the following: "ld -s -o scanAdd scanAdd.o" on my Raspbian and Kali Linux on Pi2 and it gives me the following error:

"ld: scanAdd.o: Relocations in generic ELF (EM:3) scanAdd.o: error adding symbols: File in wrong format"

Why is this error occuring? Is it because I have to setup cross-compile first from C to ARM beforehand? Need help!

User avatar
PeterO
Posts: 4862
Joined: Sun Jul 22, 2012 4:14 pm

Re: ld linking error

Thu Jun 22, 2017 7:22 am

You shouldn't need to run "ld" manually if your cross compile environment is set up correctly.

Error message look like you are trying to link x86 .o file with arm libraries or vice-versa.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

Re: ld linking error

Thu Jun 22, 2017 8:25 am

I am actually trying to run:

> nasm -f elf scanAdd.asm
> ld -m elf_i386 -s -o scanAdd scanAdd.o (using from x86)
> ./scanAdd.o

I removed the "-m elf_i386" as it is only meant to be used in x86 architecture (Windows).

How do I continue from here? Does removing the second "ld -s...." command part able to run the scanAdd.o ?

User avatar
PeterO
Posts: 4862
Joined: Sun Jul 22, 2012 4:14 pm

Re: ld linking error

Thu Jun 22, 2017 9:18 am

rlzh wrote:I am actually trying to run:

> nasm -f elf scanAdd.asm
> ld -m elf_i386 -s -o scanAdd scanAdd.o (using from x86)
> ./scanAdd.o

I removed the "-m elf_i386" as it is only meant to be used in x86 architecture (Windows).

How do I continue from here? Does removing the second "ld -s...." command part able to run the scanAdd.o ?
If you had posted that in your first message it would have been helpful because I wouldn't have replied as I have no idea about what you're trying to do !

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
Paeryn
Posts: 2604
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: ld linking error

Thu Jun 22, 2017 10:21 am

rlzh wrote:I am actually trying to run:

> nasm -f elf scanAdd.asm
> ld -m elf_i386 -s -o scanAdd scanAdd.o (using from x86)
> ./scanAdd.o

I removed the "-m elf_i386" as it is only meant to be used in x86 architecture (Windows).

How do I continue from here? Does removing the second "ld -s...." command part able to run the scanAdd.o ?
Isn't nasm just an x86/64 assembler? That will be compiling x86 assembly to an x86 object file and the arm linker is rightfully saying it can't deal with object files for x86, it's entirely the wrong architecture.
She who travels light — forgot something.

jahboater
Posts: 4585
Joined: Wed Feb 04, 2015 6:38 pm

Re: ld linking error

Thu Jun 22, 2017 10:46 am

Paeryn wrote: Isn't nasm just an x86/64 assembler? That will be compiling x86 assembly to an x86 object file and the arm linker is rightfully saying it can't deal with object files for x86, it's entirely the wrong architecture.
Correct.
Sounds like the OP will need to rewrite the source for ARM, and use the "as" assembler.
(or consider rewriting it in C making it much easier to port).

Unless you use gcc to assemble your file, then you do need the "ld" stage. Gcc will simply call "as" and then "ld" for you.

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: ld linking error

Thu Jun 22, 2017 11:05 am

Unless you use gcc to assemble your file, then you do need the "ld" stage. Gcc will simply call "as" and then "ld" for you.
The above is hard to parse. Generally, sentences that start with the word "unless" are confusing and best avoided.

Anyway, the bottom line here is that you should almost never invoke 'ld' directly. The gcc toolchain works best if you always invoke it as gcc, passing the right options to make gcc call the right programs for you.
If this post appears in the wrong forums category, my apologies.

tommylee2k
Posts: 56
Joined: Mon May 08, 2017 6:23 am

Re: ld linking error

Thu Jun 22, 2017 2:19 pm

rlzh wrote:I am actually trying to run:

> nasm -f elf scanAdd.asm
> ld -m elf_i386 -s -o scanAdd scanAdd.o (using from x86)
> ./scanAdd.o

I removed the "-m elf_i386" as it is only meant to be used in x86 architecture (Windows).

How do I continue from here? Does removing the second "ld -s...." command part able to run the scanAdd.o ?
if scanAdd.asm is x86 code, it won't run on the raspberry, this has an ARM processor. unlike C you cannot easily port this by just compile it.

Also scanAdd.o is only the object file, not the executable. you linked with "-o scanAdd", so run ./scanAdd instead

rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

Re: ld linking error

Thu Jun 29, 2017 4:24 am

Martin Frezman wrote:
Unless you use gcc to assemble your file, then you do need the "ld" stage. Gcc will simply call "as" and then "ld" for you.
The above is hard to parse. Generally, sentences that start with the word "unless" are confusing and best avoided.

Anyway, the bottom line here is that you should almost never invoke 'ld' directly. The gcc toolchain works best if you always invoke it as gcc, passing the right options to make gcc call the right programs for you.

Since never to invoke ld directly, I will have to install gcc toolchain (cross-compilation) to cross compile the source files? e.g. to cross-compile source codes on Windows to ARM (For Pi2) ?

Is it possible to install gcc on Pi2 (on the platform itself) to cross-compile source codes for x86 to ARM?

User avatar
PeterO
Posts: 4862
Joined: Sun Jul 22, 2012 4:14 pm

Re: ld linking error

Thu Jun 29, 2017 7:15 am

rlzh wrote:
Martin Frezman wrote:
Unless you use gcc to assemble your file, then you do need the "ld" stage. Gcc will simply call "as" and then "ld" for you.
The above is hard to parse. Generally, sentences that start with the word "unless" are confusing and best avoided.

Anyway, the bottom line here is that you should almost never invoke 'ld' directly. The gcc toolchain works best if you always invoke it as gcc, passing the right options to make gcc call the right programs for you.

Since never to invoke ld directly, I will have to install gcc toolchain (cross-compilation) to cross compile the source files? e.g. to cross-compile source codes on Windows to ARM (For Pi2) ?

Is it possible to install gcc on Pi2 (on the platform itself) to cross-compile source codes for x86 to ARM?
No. You can't "cross compile" or even "cross assemble" x86 code into ARM code. If you have x86 assembler code you want to run on a PI there are two options I can think of:
1) Try running the x86 executable version with an x86 emulator.
2) Recode the x86 assembler code into Arm assembler (Warning, this is a non-trivial task).

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jahboater
Posts: 4585
Joined: Wed Feb 04, 2015 6:38 pm

Re: ld linking error

Thu Jun 29, 2017 7:57 am

rlzh wrote:Is it possible to install gcc on Pi2 (on the platform itself) to cross-compile source codes for x86 to ARM?
Raspbian on the Pi2 already includes gcc pre-installed. You can use it straight away. It also of course includes the assembler (as) and the linker (ld).

BUT as PeterO says, if your code is x86 assembler then you have a much bigger problem.
(but highly educational!). Recoding in C might be a third possibility.
How big is it? Could you post a bit of the source code?

rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

Re: ld linking error

Tue Jul 04, 2017 3:47 am

jahboater wrote:
rlzh wrote:Is it possible to install gcc on Pi2 (on the platform itself) to cross-compile source codes for x86 to ARM?
Raspbian on the Pi2 already includes gcc pre-installed. You can use it straight away. It also of course includes the assembler (as) and the linker (ld).

BUT as PeterO says, if your code is x86 assembler then you have a much bigger problem.
(but highly educational!). Recoding in C might be a third possibility.
How big is it? Could you post a bit of the source code?
in fact my scanAdd file is already in .asm assembly mode, is it because I converted wrongly thats why its not working? Rather I have to recode again to suit Pi2 platform (as different binary compared to Windows)

User avatar
Paeryn
Posts: 2604
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: ld linking error

Tue Jul 04, 2017 11:14 am

rlzh wrote:in fact my scanAdd file is already in .asm assembly mode, is it because I converted wrongly thats why its not working? Rather I have to recode again to suit Pi2 platform (as different binary compared to Windows)
By the sounds of it your assembly file is written in x86 assembly (Intel/AMD), that is totally different to ARM assembly which is needed for the RPi. Your assembly code needs to be rewritten for the ARM processor.
She who travels light — forgot something.

rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

Re: ld linking error

Fri Jul 07, 2017 5:30 am

Paeryn wrote:
rlzh wrote:in fact my scanAdd file is already in .asm assembly mode, is it because I converted wrongly thats why its not working? Rather I have to recode again to suit Pi2 platform (as different binary compared to Windows)
By the sounds of it your assembly file is written in x86 assembly (Intel/AMD), that is totally different to ARM assembly which is needed for the RPi. Your assembly code needs to be rewritten for the ARM processor.
Now I've switched back to Windows and used IDAPro to convert .exe to .asm to be assembled using nasm, but since then I've encountered problems trying to assemble with the command "nasm -f elf scan.asm" and "nasm -f win32 scan.asm -o scan.o"

User avatar
Paeryn
Posts: 2604
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: ld linking error

Fri Jul 07, 2017 11:19 am

I'm getting very confused over what it is you are trying to achieve. What is scanAdd.asm, what is this windows .exe, why are you disassembling it and then trying to reassemble it? Where does the RPi come in to this?
She who travels light — forgot something.

User avatar
RaTTuS
Posts: 10365
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: ld linking error

Fri Jul 07, 2017 11:25 am

x86 assembler is not the same as arm assembler
.c .cpp etc can be re-compiled easily [YMMV]
python can be run [YMMV]

if you have written x86 assembler [and who would since the days of the pentium] then you ate going to have to re-write
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

Re: ld linking error

Mon Jul 10, 2017 5:45 am

Paeryn wrote:I'm getting very confused over what it is you are trying to achieve. What is scanAdd.asm, what is this windows .exe, why are you disassembling it and then trying to reassemble it? Where does the RPi come in to this?
I'm actually trying to run Heap Spray application to scan for memory addresses, but the default .c files are written in x86 format (Windows) which i am trying to pot it over to RPi2 (ARM) which I will have to rewrite the .c files (am not very advanced in C though)

rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

Re: ld linking error

Mon Jul 10, 2017 5:46 am

RaTTuS wrote:x86 assembler is not the same as arm assembler
.c .cpp etc can be re-compiled easily [YMMV]
python can be run [YMMV]

if you have written x86 assembler [and who would since the days of the pentium] then you ate going to have to re-write
Yes I have to rewrite the original codes, but its not so straightforward as it is though, different architecture have different binaries which are somehow complicated to understand. Right now I am trying to test it on Windows, which nasm is giving me some errors after converting the C to Assembly language

jahboater
Posts: 4585
Joined: Wed Feb 04, 2015 6:38 pm

Re: ld linking error

Mon Jul 10, 2017 7:15 am

None of this makes any sense!

Please could you post this code.

rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

Re: ld linking error

Mon Jul 10, 2017 7:48 am

jahboater wrote:None of this makes any sense!

Please could you post this code.
I am currently using IDA Pro to generate .asm files from the original .exe executable (from C), which nasm nor masm as what I am been trying out all along couldnt work due to the asm output files have more than 1K lines of Assembly codes, any suggestions or help to solve this issue?

jahboater
Posts: 4585
Joined: Wed Feb 04, 2015 6:38 pm

Re: ld linking error

Mon Jul 10, 2017 8:28 am

rlzh wrote:I am currently using IDA Pro to generate .asm files from the original .exe executable (from C), which nasm nor masm as what I am been trying out all along couldnt work due to the asm output files have more than 1K lines of Assembly codes, any suggestions or help to solve this issue?
So you have an executable (originally written in C) which you have disassembled to produce a .asm file.

Have you got access to the original C code?

If so, your task should be (relatively) easy.
If not, its a nightmare!

Assembler source code, especially Intel, can have many variants accepted by different assemblers - even for the same CPU architecture.

The fact that you have tried "masm" (Microsoft) implies you are trying to assemble the disassembled output back to an x86 program again which seems pointless. And I think nasm is Intel only too. The assembler on the Pi is called "as".

To run it on the Pi ...
As has been pointed out many times now, if you don't have the original C source, you will have to translate the x86 assembler (.asm) to ARM assembler, line by line. A very difficult task. There will be no comments and few symbols in the disassembled .asm, and x86 is a CISC architecture (complex instruction set) while ARM is RISC (reduced instruction set), meaning that many of the Intel x86 instructions will translate into several ARM instructions.

If you do have the original C source, you can use the Pi's C compiler (gcc).

User avatar
RaTTuS
Posts: 10365
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: ld linking error

Tue Jul 11, 2017 7:19 am

rlzh wrote:
Paeryn wrote:I'm getting very confused over what it is you are trying to achieve. What is scanAdd.asm, what is this windows .exe, why are you disassembling it and then trying to reassemble it? Where does the RPi come in to this?
I'm actually trying to run Heap Spray application to scan for memory addresses, but the default .c files are written in x86 format (Windows) which i am trying to pot it over to RPi2 (ARM) which I will have to rewrite the .c files (am not very advanced in C though)
so your trying to write code to exploit vulnerabilities in other peoples software , via the use of software that you don't have the source code to .
this is not well thought out , and the methodology will not work as you think it does,
you will spend more time learning how arm assembler works [this is a good thing] but you will not achieve what you think you want.
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

Re: ld linking error

Tue Jul 11, 2017 8:10 am

RaTTuS wrote:
rlzh wrote:
Paeryn wrote:I'm getting very confused over what it is you are trying to achieve. What is scanAdd.asm, what is this windows .exe, why are you disassembling it and then trying to reassemble it? Where does the RPi come in to this?
I'm actually trying to run Heap Spray application to scan for memory addresses, but the default .c files are written in x86 format (Windows) which i am trying to pot it over to RPi2 (ARM) which I will have to rewrite the .c files (am not very advanced in C though)
so your trying to write code to exploit vulnerabilities in other peoples software , via the use of software that you don't have the source code to .
this is not well thought out , and the methodology will not work as you think it does,
you will spend more time learning how arm assembler works [this is a good thing] but you will not achieve what you think you want.
the source codes which you are referring to is it the original .c files (written in x86) ? Yes I understood that I am required to rewrite to fit into ARM, which I am not very good in C programming

rlzh
Posts: 15
Joined: Tue Jun 06, 2017 9:42 am

Re: ld linking error

Tue Jul 11, 2017 8:12 am

jahboater wrote:
rlzh wrote:I am currently using IDA Pro to generate .asm files from the original .exe executable (from C), which nasm nor masm as what I am been trying out all along couldnt work due to the asm output files have more than 1K lines of Assembly codes, any suggestions or help to solve this issue?
So you have an executable (originally written in C) which you have disassembled to produce a .asm file.

Have you got access to the original C code?

If so, your task should be (relatively) easy.
If not, its a nightmare!

Assembler source code, especially Intel, can have many variants accepted by different assemblers - even for the same CPU architecture.

The fact that you have tried "masm" (Microsoft) implies you are trying to assemble the disassembled output back to an x86 program again which seems pointless. And I think nasm is Intel only too. The assembler on the Pi is called "as".

To run it on the Pi ...
As has been pointed out many times now, if you don't have the original C source, you will have to translate the x86 assembler (.asm) to ARM assembler, line by line. A very difficult task. There will be no comments and few symbols in the disassembled .asm, and x86 is a CISC architecture (complex instruction set) while ARM is RISC (reduced instruction set), meaning that many of the Intel x86 instructions will translate into several ARM instructions.

If you do have the original C source, you can use the Pi's C compiler (gcc).
Yes nasm and masm is in Intel, but masm as what I understood is easier than nasm, which what you had mentioned earlier it defits the purpose with assembling/disassembling the .asm output file from IDA Pro, but rather somehow, I am tasked to produce an executable (from IDA Pro) which is to run the application on both Windows and RPi2, which I am currently stucked at Windows for now

User avatar
topguy
Posts: 5610
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: ld linking error

Tue Jul 11, 2017 3:34 pm

rlzh wrote: the source codes which you are referring to is it the original .c files (written in x86) ? Yes I understood that I am required to rewrite to fit into ARM, which I am not very good in C programming
C-code is very seldom "written in x86", the language is made to be portable. So unless there a great amounts of inline x86-assembly in the code you will be much more concerned about differences in the operating systems (Linux and Windows) than CPU architecture. And programs are very often written for (one or more) specific Operating Systems.

In your stackoverflow thread you mention that #include "windows.h" fails, and this tells me that it will be the bigger obstacles you must handle.
If this program is a GUI program for Windows then all of that GUI code needs to be rewritten in a GUI framework for Linux, and converting to Assembler code will NEVER fix this for you.
If this is a commandline tool, then maybe the programmer included "windows.h" just for some convenient macros and those can usually be ported to Linux without too much effort.
I am tasked to produce an executable (from IDA Pro) which is to run the application on both Windows and RPi2, which I am currently stucked at Windows for now.
Your task is not in the realm of "possible" at the moment in my opinion. This sounds like completely the wrong way to port a program to the Pi.

Return to “C/C++”