jaskdjfasdhfawlg
jaskdjfasdhfawlg

Reputation: 21

gcc assembler - create only the minimal instructions necessary

I have created a very minimal application in assembly. It sets some registers to 0 and does a multiplication. Nothing fancy.

However, the gcc adds a lot of stuff to the machine code I do not want.

A small list of the stuff I find in the objdump:

I know that I do not need them, but I have no idea how I can tell to compiler to stop including them. I tried to use optimization options, but this did not change anything.

I compile it basically like: GCC -o ./main.elf ./main.S

Thank you very much for any help!

Upvotes: 1

Views: 1434

Answers (2)

old_timer
old_timer

Reputation: 71516

so.S:

nop
nop

then build.

as so.S -o so.o
ld -Ttext=0x1000 so.o -o so.elf
objdump -D so.elf

Disassembly of section .text:

0000000000001000 <__bss_start-0x200002>:
    1000:   90                      nop
    1001:   90                      nop

objcopy -O binary so.elf so.bin
hexdump -C so.bin
00000000  90 90                                             |..|
00000002

using gcc

gcc -nostartfiles -nostdlib -nodefaultlibs -ffreestanding so.S -Xlinker "-Ttext=0x1000" -o so.elf

this leaves extra garbage in the file, but

gcc so.S -c -o so.o
ld -Ttext=0x2000 so.o -o so.elf
ld: warning: cannot find entry symbol _start; defaulting to 0000000000002000
objdump -D so.elf


Disassembly of section .text:

0000000000002000 <__bss_start-0x200002>:
    2000:   90                      nop
    2001:   90                      nop

But if writing assembly language you might as well use the assembler not the compiler.

_start is not required unless you need an entry point defined in the file then you need to do this:

.globl _start
_start:

plus possibly something in the linker to call that out as the entry point for file formats like elf, exe, etc.

works for cross compiling as well

arm-none-eabi-as so.s -o so.o
arm-none-eabi-ld -Ttext=0x3000 so.o -o so.elf
arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000003000
arm-none-eabi-objdump -D so.elf

so.elf:     file format elf32-littlearm


Disassembly of section .text:

00003000 <__bss_end__-0x10008>:
    3000:   e1a00000    nop         ; (mov r0, r0)
    3004:   e1a00000    nop         ; (mov r0, r0)


pdp11-aout-as so.s -o so.elf
pdp11-aout-as so.s -o so.o
pdp11-aout-ld -Ttext=0x400 so.o -o so.elf
pdp11-aout-objdump -D so.elf

so.elf:     file format a.out-pdp11


Disassembly of section .text:

00000400 <so.o>:
 400:   00a0            nop
 402:   00a0            nop

and so on.

Upvotes: 2

Clifford
Clifford

Reputation: 93456

GCC automatically links the C / C++ runtime start-up crt0.o and the standard library. You can provide your own startup code to override the default and provide command line options to force it not to link no the standard library.

Options controlling startup and default libraries include:

-nostartfiles
-nostdlib
-nodefaultlubs
-nolibc

Each affects the link in a different way, but in this case -nostdlib will exclude both crt0.o and standard libraries. Of course if your code makes no reference to the standard library then nothing will be linked in any case, but explicitly excluding it will helpfully generate a link error if something does reference it.

See: https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

Be aware that if your code does not establish a valid C runtime environment, providing for example static initialisation and a stack (a minimum), then some C code may not run in the manner intended. You may also need to specify the entry point via --entry=entry if you do not use the same default entry point as crt0 (_start I think).

Alternatively you can invoke gcc with the -c option and separately invoke the linker ld without specifying any library.

Upvotes: 3

Related Questions