“Illegal instruction” on basic assembly program - not even hello world - why is linking needed?

StackOverflow https://stackoverflow.com/questions/9815864

Pergunta

I just figured this out but instead of splitting my new question ("why?") into another question I think its best if the solution to this problem and an explanation were to be kept on the same page.

I'm writing a basic assembly program to just start and immediately quit using the kernel interrupt at int 0x80. My current code is simply as follows:

/* Simple exit via kern-interrupt */

.globl start

start:
    pushl $0x0
    movl $0x1, %eax
    subl $4, %esp
    int $0x80

assembled with

as -arch i386 <file>.s

upon executing I get a one-line error:

Illegal instruction

It's bizzare, even commenting everything out still results in Illegal instruction despite there being no instructions at all. Am I missing a linking step, despite there being no other files to link to? Yes I am

EDIT: Allow me to rephrase my question, why do you need to link when there is no library or anything to link to?

Foi útil?

Solução

You do need to link it to create an executable. By default, as just gives you an object file, which is something you can link into an executable (either with other object files or on its own) but is not itself a valid executable. Try:

as -arch i386 -o file.o file.s
ld -o file file.o

In answer to your question:

Why do you need to link when there is no library or anything to link to?

Because the assembler doesn't know that you're not going to link with something else.

Unlike the gcc compiler where it assumes you want a program unless told otherwise (with the -c option), as gives you an object file by default. From the manpage:

"as" is primarily intended to assemble the output of the GNU C compiler "gcc" for use by the linker "ld"

If you want a one-step command, you can create a script such as asld:

as -arch i386 -o $1.o $1.s
ld -o $1 $1.o

and then just use asld file.

Or, you could set up makefiles to do all the heavy lifting for you.

Outras dicas

You could make the same argument about a C program, I am not using any libraries why do I have to link.

Because that is how the toolchain was designed. One set of tools takes you from source code (any/many languages) to object files which are most of the time incomplete. The link stage, even if as paxdiablo shows, only takes your object file and makes it an executable, is required. If nothing else your .text address is (usually) needed and that comes from the linker stage.

It makes a lot of sense to do it this way, the link stage is complicated enough as it is, make that one tool that does that job and is good at that job. Do your system engineering and define an interface to that tool. The language tools have a complicated job to do have them just do that job, the output being an object file, which is as far as they can resolve without having to become a linker.

If you wish to not use this toolchain and perhaps use nasm or something like that where you can go directly from assembly to binary in one command line step.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top