07.04.2013 Views

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

16-Bit C <strong>Compiler</strong> User’s Guide<br />

4.7 LOCATING CODE AND DATA<br />

As described in Section 4.3 “Address Spaces”, the compiler arranges <strong>for</strong> code to be<br />

placed in the .text section, <strong>and</strong> data to be placed in one of several named sections,<br />

depending on the memory model used <strong>and</strong> whether or not the data is initialized. When<br />

modules are combined at link time, the linker determines the starting addresses of the<br />

various sections based on their attributes.<br />

Cases may arise when a specific function or variable must be located at a specific<br />

address, or within some range of addresses. The easiest way to accomplish this is by<br />

using the address attribute, described in Section 2.3 “Keyword Differences”. For<br />

example, to locate function PrintString at address 0x8000 in program memory:<br />

int __attribute__ ((address(0x8000))) PrintString (const char *s);<br />

Likewise, to locate variable Mabonga at address 0x1000 in data memory:<br />

int __attribute__ ((address(0x1000))) Mabonga = 1;<br />

Another way to locate code or data is by placing the function or variable into a<br />

user-defined section, <strong>and</strong> specifying the starting address of that section in a custom<br />

linker script. This is done as follows:<br />

1. Modify the code or data declaration in the C source to specify a user-defined<br />

section.<br />

2. Add the user-defined section to a custom linker script file to specify the starting<br />

address of the section.<br />

For example, to locate the function PrintString at address 0x8000 in program<br />

memory, first declare the function as follows in the C source:<br />

int __attribute__((__section__(".myTextSection")))<br />

PrintString(const char *s);<br />

The section attribute specifies that the function should be placed in a section named<br />

.myTextSection, rather than the default .text section. It does not specify where<br />

the user-defined section is to be located. That must be done in a custom linker script,<br />

as follows. Using the device-specific linker script as a base, add the following section<br />

definition:<br />

.myTextSection 0x8000 :<br />

{<br />

*(.myTextSection);<br />

} >program<br />

This specifies that the output file should contain a section named .myTextSection<br />

starting at location 0x8000 <strong>and</strong> containing all input sections named.myTextSection.<br />

Since, in this example, there is a single function PrintString in that section, then the<br />

function will be located at address 0x8000 in program memory.<br />

Similarly, to locate the variable Mabonga at address 0x1000 in data memory, first<br />

declare the variable as follows in the C source:<br />

int __attribute__((__section__(".myDataSection"))) Mabonga =<br />

1;<br />

DS51284H-page 68 © 2008 <strong>Microchip</strong> Technology Inc.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!