String alignment issue

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

String alignment issue

1,435 Views
CarlFST60L
Senior Contributor II

Hi,

 

(CW7.1.2 patch build 11)

 

How do I get my string alignment to stop adding extra bytes in the following example?

 

Example:

 

nvm.c

 

volatile uchar ID1[12] = "123456789";

volatile uchar ID2[12] = "987654321";

 

linker:

 

 .NVM_Page:
 {
  nvm.c (.data)
  nvm.c (.rodata)
  nvm.c (.text)
 } > my_NVM

 

map file:

10060026 0000000C .data   ID1(nvm.c)
10060034 0000000C .data   ID2(nvm.c)

 

The map file shows ID2 at 0x10060034 instead of 0x10060032. If I change ID1 array to ID1[14], ID2 array is still at 10060034. Single byte varibles, 2 byte varibles are fine, its just arrays as far as i can tell.

 

What do I need to do to fix this issue?

Labels (1)
0 Kudos
4 Replies

411 Views
jbezem
Contributor III

You may want to review your linker settings and LCF file with respect to the notes in TN256 "Converting Projects to
CodeWarrior™ ColdFire® V7.0", sections "Performance Improvements": "Alignment" and "Backwards Compatibility".

I didn't check the details, but from there I suspect that a 32-bit alignment is default, since both variables are put each in their own ELF-section, and those sections seem padded to 4-byte boundaries.

It's still possble that the linker is producing wrong allocation, depending on your LCF, IMHO.

 

HTH,

 

Johan

0 Kudos

411 Views
CompilerGuru
NXP Employee
NXP Employee

I was not able to reproduce this.

With this code:

 

 

unsigned char ID0 = 1;volatile unsigned char ID1[12] = "12345678";volatile unsigned char ID2[12] = "98765432";int main(void) { ID0++; ID1[0] = ID2[1]; for(;;) {     }}

 

 

I get

 

  2000F000 00000001 .data   ID0 (main.c)  2000F001 0000000C .data   ID1 (main.c)  2000F00D 0000000C .data   ID2 (main.c)

 So no alignment, ID2 starts at an odd address.

 

Daniel

 

0 Kudos

412 Views
CarlFST60L
Senior Contributor II

Thanks for checking that..

 

I also get the same results as you if I use RAM and the standard linker RAM settings, however, I have created my own section in memory and used the above linker code to get the data where i want it in memory. (Its actually in MRAM on the 52259evb).

 

I think possibly the linker code is aligning the data incorrectly

 

 

0 Kudos

412 Views
CompilerGuru
NXP Employee
NXP Employee

Not sure, can you create a project which shows the behavior?

Note that in ELF, the alignment is defined for sections, not for individual objects. Therefore the linker may just not know that a 12 byte object does not need a 4 alignment if the same section contains objects which need a 4 byte alignment. Therefore placing the strings into their own section might help.

 

Daniel

 

0 Kudos