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.

VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Jan 28, 2019 at 11:05
6
  • Note: app_start won't be called even if i call directly (calling app_start() shown in comment, instead of asm "call 0xFA0") Commented Jan 28, 2019 at 11:07
  • You mean -o ubb.elf? Commented Jan 28, 2019 at 12:15
  • but the version in question with app_start called from main works? Commented Jan 28, 2019 at 12:25
  • No, calling app_start directly from the main also doesn't work. Commented Jan 28, 2019 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. Commented Jan 28, 2019 at 12:37

1 Answer 1

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)

answered Jan 29, 2019 at 9:51
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.