Link script .lds file hasn't effect but compile successfully

0

Here is the code

//hello.c
char* str = "Hello world!\n";

void print()
{
    asm(
        "mov $13, %%rdx \n\t"
        "mov %0,  %%rcx \n\t"
        "mov $0,  %%rbx \n\t"
        "mov $4,  %%rax \n\t"
        "int $0x80       \n\t"
        ::"r"(str):"rdx","rcx","rbx");
}

void exit()
{
    asm(
        "mov $42, %rbx \n\t"
        "mov $1,  %rax \n\t"
        "int $0x80      \n\t"
    );
}

void hello()
{
    print();
    exit();
}

And here is the link script hello.lds

ENTRY(hello)

SECTIONS
{
    . = 0X08048000 + SIZEOF_HEADERS;
    
    tinytext : {*(.text) *(.data) *(.rodata)}
    
    /DISCARD/ : {*(.comment)}
}

I use the following command to compile and build an ELF file, and I want to combine .text, .data and .rodata to one .tinytext, what's more, I want to discard .comment.

gcc -c -fno-builtin hello.c
ld -static -T hello.lds -o hello hello.o

But the thing is that this can not work, when using objdump or readelf, I find the .text .data and .rodata still exist. What's more, the ELF file size enlarged from 1.6KB to 296KB(What I intended for is that the size may be less than 1KB).

Is there any problem of my operation?

And when I change the addr from 0x08048000 to 0x00000000, I find the size decrease from 296KB to a relatively small size like 1.1KB, can anyone explain it?

Thanks for any help.

c
linux
assembly
linker
asked on Stack Overflow Jan 8, 2021 by tyChen

1 Answer

1

After the help from Jester and Peter Cordes, I solve the problem. This problem comes from the wrong use of lds. In 64bit program, the general base address is not 0x08048000 but 0x400000. Changing that address, I successfully decrease the size of ELF file to 700 bytes and the result of objdump is just what I want to have.

answered on Stack Overflow Jan 11, 2021 by tyChen

User contributions licensed under CC BY-SA 3.0