2

I wrote the following code:

#include <avr/io.h>

__attribute__((noinline, section(".app_start"))) void app_start() //app_start section starts at 0xFA0
{
    //register_packet_dispatch(packet_received);
    PORTB = 0xFF;
}

__attribute__ ((noinline)) void call_app()
{
    asm ("call 0xFA0\nret");
    //app_start();
}

int main()
{
    DDRB = 0xFF;
    //call_app();
    app_start();
    while(1){}
}

What I compiled with:

avr-gcc -Wl,--section-start=.reg_dispatch=0xdac -Wl,--section-start=.app_start=0xFA0 -mmcu=atmega328p -Os -o ubb.o ub_bootloader.cpp -DF_CPU=16000000

Which tries to simulate a host application that tries to call a relocated client application, begins from the 4000 address (0xFA0). If it would work, it's light up the LED wired to the arduino's 13. pin. But why app_start won't be called?

I've also inspected the output assemlby:

avr-objcopy -j .text -j .data -O ihex ubb.o ubb.hex

00000fa0 <_Z9app_startv>:
 fa0:   8f ef           ldi r24, 0xFF   ; 255
 fa2:   85 b9           out 0x05, r24   ; 5
 fa4:   08 95           ret

00000080 <main>:
  80:   8f ef           ldi r24, 0xFF   ; 255
  82:   84 b9           out 0x04, r24   ; 4
  84:   0e 94 d0 07     call    0xfa0   ; 0xfa0 <_Z9app_startv>
  88:   ff cf           rjmp    .-2         ; 0x88 <main+0x8>

Everything seems correct, why it isn't working at all? Of course, if i remove the "noinline" attribute, it works. But that's a mirage, the compiler inlines the content of the function, but when I build this into an application, the code after the 0xFA0 address will be rewritten by the host application.

  • Note: app_start won't be called even if i call directly (calling app_start() shown in comment, instead of asm "call 0xFA0") – Dankó Dávid Jan 28 '19 at 11:07
  • You mean -o ubb.elf? – Edgar Bonet Jan 28 '19 at 12:15
  • but the version in question with app_start called from main works? – Juraj Jan 28 '19 at 12:25
  • No, calling app_start directly from the main also doesn't work. – Dankó Dávid Jan 28 '19 at 12:34
  • ubb.o: .elf and .o are just file endings file ubb.o says: ubb.o: ELF 32-bit LSB executable, Atmel AVR 8-bit, version 1 (SYSV), statically linked, not stripped. – Dankó Dávid Jan 28 '19 at 12:37
3

I've tried this code with arduino and it worked (i taught, of course, i can't add the "-Wl,--section-start=.app_start=0xFA0" flag to the compilation process so the function not gonna be moved to other section) So i set the arudino IDE to show every command from the compilation to the end of the upload process.

The problem lies in the usage of avr-objdump, whose i found somewhere and used witouth overview the CLI switches.

I've also made a mistake when i wrote the question. To disassemble the object file, the right command is:

avr-objdump -S --disassemble  ubb.o > ubb.asm

THe command i've used to create hex file:

avr-objcopy -j .text -j .data -O ihex ubb.o ubb.hex

The working one (extracted from arduino):

avr-objcopy -O ihex -R .eeprom ubb.o ubb.hex

-j means keep only the given section(s)

while

-R means, remove only the given section(s)

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .