Assigning fixed absolute address for a function?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Assigning fixed absolute address for a function?

2,383 Views
dkelly
Contributor I
Plenty of examples floating around for placing a variable at a specific memory address but finding examples of controlling the address of a function to be lacking. At least in the 8/16 side of the house.

Have inherited a bootloader in an S12NE64 that calls a function at 0x40a2. Would like to discuss options on how to place my function at that address.

So far have created a SEGMENT and used #pragma to place my function in that CODE_SEG.

COMPAT_40a2 = READ_ONLY 0x0040a2 TO 0x007fff FILL 0xff;

The above under SEGMENTS, the following under PLACEMENT doesn't seem right, but works:

COMPAT_40a2 INTO COMPAT_40a2;

This works, but it doesn't have the feel of control that I desire. The only reason my function is at 0x40a2 is that its the only one in that SEGMENT. OK, I can manually adjust the size of the segment and keep that routine as the only one placed there, but I'd like some cleaner means of control. Is there?
Labels (1)
Tags (1)
0 Kudos
Reply
3 Replies

903 Views
CrasyCat
Specialist III
Hello
 
using a user defined section and placing it in the appropriate address is the only way you can get a function allocated at an absolute address.
 
There is no reason to manually adjust the size of the section to the function length.
In your PRM file you can tell that you want to place this section plus some others in that segment.
 
For instance in your placement BLOCK you can write:
  COMPAT_40a2, AnotherSectio, OneMoreSection  INTO COMPAT_40a2;
Further more you can even write
  COMPAT_40a2, AnotherSection, OneMoreSection  INTO COMPAT_40a2, AnotherSegment;
Function stored in COMPAT_40a2 will always be allocated in COMPAT_40a2. Then the linker will fill the rest of the code into the specified segments.
 
CrasyCat

903 Views
dkelly
Contributor I
"Function stored in COMPAT_40a2 will always be allocated in COMPAT_40a2."

I'm concerned about your use of "in" because I'm looking for "at". Can't be "in" the general volume of COMPAT_40a2, I need the function to start exactly at 0x40a2.

Am guessing you are saying that I should define a section, say, MUST_BE_0x40a2 and #pragma CODE_SEG MUST_BE_0x40a2 my function. And only that function, no others.

Then define a SEGMENT which starts at 0x40a2 and list my MUST_BE_0x40a2 SECTION as the first member and will be assured that its always placed first at 0x40a2? This seems to work.

The argument to #pragma CODE_SEG doesn't have to exist to compile, only to link. At link time if its not listed in PLACEMENT it silently falls into DEFAULT_ROM.


Am a bit fuzzy as to the difference between SECTIONS and SEGMENTS. The words seem interchangeable in the .PRM file and elsewhere. Digging through the help system I initially thought SEGMENTS were an ELF thing and SECTIONS were a Freescale/Hiwire thing. Then found enlightening statements to the effect, "Sections go in segments. Section is a group of objects. A segment is a memory region which is not necessarily contiguous."

So I'm thinking "#pragma CODE_SEG" accepts either a segment or a section as its argument?

That a section is a Freescale thing. That multiple sections can be grouped into an ELF segment when the object file is written.
0 Kudos
Reply

903 Views
CompilerGuru
NXP Employee
NXP Employee
The term "section" is a used by the ELF object file format definition, so I'm using it for the names of the "sections" in the ELF file.
Therefore a #pragma CONST_SEG is expecting a section name, and those sections names are listed in the prm in the PLACEMENT area, where the content of each section is assigned into possibly multiple segments.
So segment names only appear in the prm where they name a particular memory area in a SEGMENTS block, and where they are used on the right side of one or multiple entries in the PLACEMENT block on the right side of the INTO.

So that is how I think the terminology should be used for ELF. Note that historically in the  today not used HIWARE object file format, the terms segment and section have been used differently, and that is a reason where a some of the confusion is originated.
One such topic, for example is why the pragma is called CONST_SEG (and not CONST_SEC), that is just because how the terms have been used initially with the HIWARE object file format and the pragma name was kept when switching to ELF later on.

Note also that while you can use the same name for segments as for sections as in the original post, I would usually not recommend that. By using a distinct name, it is clear which identifiers have to match, and which ones not.
So I would rather use COMPAT_40A2_SECTION INTO COMPAT_40A2_SEGMENT, or something like that.


Daniel