Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link a C object file with a Assembly Language object file?

I am having trouble linking 2 object files one of which was generated from an Assembly Language Source File and another that was generated from a C Source file.

C source code:

//main2.c
extern int strlength(char *);
int main(){
    char * test = "hello";
    int num = strlength(test);
    return num;
}

Assembly source code:

#strlength.s
.include "Linux32.s"

.section .text
.globl strlength
.type strlength, @function
strlength:
 pushl %ebp
 movl %esp, %ebp
 movl $0, %ecx
 movl 8(%ebp), %edx
read_next_byte:
 movb (%edx), %al
 cmpb $END_OF_FILE, %al
 jle end
 incl %edx
 incl %ecx
 jmp read_next_byte
end:
 movl %ecx, %eax
 popl %ebp
 ret

When I compile and run using 'gcc' like this:

gcc main2.c strlength.s -m32 -o test
./test
echo $?

I get 5 which is correct. However when I compile/assemble separately and then link with 'ld' like this:

as strlength.s --32 -o strlength.o
cc main2.c -m32 -o main2.o
ld -melf_i386 -e main main2.o strlength.o -o test
./test

I get a segmentation fault. What is causing this? Am I not following the C calling convention 100% correctly?

like image 979
Hudson Worden Avatar asked Dec 31 '11 23:12

Hudson Worden


People also ask

How object files are linked?

A shared object file holds code and data suitable to be linked in two contexts. First, the link-editor can process it with other relocatable and shared object files to create other object files. Second, the runtime linker combines it with a dynamic executable file and other shared objects to create a process image.

Is assembly language an object file?

An object file is a computer file containing object code, that is, machine code output of an assembler or compiler. The object code is usually relocatable, and not usually directly executable. There are various formats for object files, and the same machine code can be packaged in different object file formats.

What is .OBJ file in assembly language?

8.3. The assembler is used to convert an assembly file into an object file (. obj extension). The assembly optimizer and the compiler are used to convert, respectively, a linear assembly file and a C file into an object file. The linker is used to combine object files, as instructed by the linker command file (.

What is object file linker?

In computing, a linker or link editor is a computer system program that takes one or more object files (generated by a compiler or an assembler) and combines them into a single executable file, library file, or another "object" file.


1 Answers

ld -melf_i386 -e main main2.o strlength.o -o test

Don't do that. Do this instead:

gcc -m32 main2.o strlength.o -o test

(You should probably not call your test exectuable test, as it may conflict with the /bin/test, standard on most UNIX systems.)

Explanation: UNIX binaries do not generally start executing at main. They start executing in a function called _start, which comes from crt1.o or similar ("C Runtime startup"). That file is part of libc, and it arranges for various initializations, required for the proper start up of your application.

Your program actually doesn't require anything from libc, which is why you were able to link it with ld.

However, consider what happens after your main returns. Normally, the code in crt1.o will execute (equivalent of) exit(main(argc, argv));. Since you linked without crt1.o, there is nobody to do that final exit for you, so the code returns to ... undefined location and promptly crashes.

like image 71
Employed Russian Avatar answered Oct 22 '22 13:10

Employed Russian