Question

This experiment is on the 32 bit Linux.

I want to do a transformation on the asm level, and I am trying to implement my transformation before the function main is called.

Currently I am trying to program a new entry point, implement my transformation code, and hope this new entry point can successfully call main

Basically the default entry point of gcc generated assembly code is main, which I give an example as follow:

c code:

int main()
{
    return 0;
}

I use this command to generate asm code:

gcc -masm=intel -fno-asynchronous-unwind-tables -S main.c

and this is what I got:

    .file   "main.c"
    .intel_syntax noprefix
    .text
    .globl  main
    .type   main, @function
main:
    push    ebp
    mov     ebp, esp
    mov     eax, 0
    pop     ebp
    ret
    .size   main, .-main
    .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
    .section        .note.GNU-stack,"",@progbits

Could anyone tell me how to implement a new entry point(probably a function similiar like _start) and call main at the end of this new entry point?

Thank you!

Was it helpful?

Solution

I doubt you should replace _start() because it's very platform- and libc-specific. Either you write all code in assembler and so you don't need libc-specific initialization, or you should copy all _start() activity including things you aren't aware. The latter looks simply bogus.

If you agree not to replace start() but use a mechanism to run some code before main(), declare a function with __attribute__((constructor)). This is documented GCC extension and it's actively used e.g. for static object initializing in C++. Such function can't get arguments or return a real value, nor shall it override control flow in another way. I can't catch what you mean for "transformation" so it can contradict to your intention; if so, you would have explained this more detailedly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top