09202024

Last update2016/05/28 14:38

2.PIC: Position Independent Code*位置独立コードは使えますか?

 2.PIC: Position Independent Code*位置独立コードは使えますか?

If developers for instance is working on a bootloader, position independency is a great help. PIC (Position Independent Code) is relative to the program counter, so if you compile for address 0 but place the code at 81000 it still runs properly.

The compiler has an option

-fPIE 

that enables the compiler to generate position independent code for executable. Add this option into the tool settings for the Assembler and C Compiler in the miscellaneous settings for C/C++ Build.


Also use this -fPIE option for the linker. E.g.  in the miscellaneous settings the "Other options" field for the C linker, the command may look like

-Wl,-cref,-u,Reset_Handler -fPIE 


Developer must also make sure that the stack pointer is set up correctly. This is usually done by issuing a monitor reset when launching the debug session; that will trigger the processor to read the stack pointer from the first entry in the interrupt vector.

Developer can make sure that the start code when starting also sets the stack pointer; do this by adding the following assembly line at the top of the Reset_Handler function located in the startup file:

  ldr sp, =_estack

This will make sure that the stack pointer is initialized when the reset_handler runs.

Developer can also try removing the initial reset command that we issue when launching the debug session. 
Do this by opening your debug configuration: Select the project in the project explorer view, then select the Run -> Debug configurations menu alternative.
Select the debug configuration, switch to the Startup Debug tab to the right.
Here developer can see all commands that we use to launch a debug session. Try commenting the monitor reset line out by adding a # sign in front.

Also in some of our examples developer will have to make some changes in the SystemInit function for the vector table relocation.

SCB->VTOR = FLASH_BASE | 0x20000; /* Vector Table Relocation in Internal FLASH */

If not, this SystemInit function will relocate interrupts to flash beginning.

To make sure the code is started where is should be, comment out the "continue" command from the startup script.
This will suspend execution on the first instruction in the reset handler, making it possible to debug the start-up code.

1..hex-fileは作れますか?

 

1..hex-fileは作れますか?

To convert your program to another output format, do the following:

 

1) Open up Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> Other-> Output format
 
2) Check the box Convert build output and choose a format in the dropdown menu.
Output settings
3) Build your project
The converted output will be located in your output directory associated with the currently active build configuration (typically Debug/Release directory).