This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Char array and .data section

Note: This was originally posted on 11th January 2011 at http://forums.arm.com

Hi,

I've been trying to use string literals in my C code, however I'm having a really odd problem.

I've condensed my code to do just a basic store string literal in a char variable, and output element value:

Code 1:

#include "generic.h"
#include "stdlib.h"
#include "string.h"

int main(void) {

char chartest[4]="HEL";
uart_send_num(chartest[2]); //function I've written to output the numeric value of data, to my serial port

}

Result 1:
0076

So that result is what I expected to see (ASCII : L = 76).

If I change the array size to anything bigger than 4 elements:

Code 2:

#include  "generic.h"
#include "stdlib.h"
#include "string.h"

int main(void) {

char chartest[5]="HELL";
uart_send_num(chartest[2]);

}

Result 2:
0000

I can't understand why this would be happening.
I'm using Codesourcery gcc with Eclipse SDK, but still trying to figure out how everything works, so I haven't got all the libraries and maybe even linker script working correctly, i.e. printf, malloc, etc. doesn't work, therefore I wrote my own uart routine.

Part of my linker script is as follows:

.text : {
     CREATE_OBJECT_SYMBOLS
     *(.text .stub .text.* .gnu.linkonce.t.*)
     *(.plt)
     *(.gnu.warning)
     *(.glue_7t) *(.glue_7)      /* NOTE: placed already in .fastcode */

     . = ALIGN (4);
     /* These are for static constructors and destructors under ELF */
     KEEP (*crtbegin.o(.ctors))
     KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
     KEEP (*(SORT(.ctors.*)))
     KEEP (*crtend.o(.ctors))
     KEEP (*crtbegin.o(.dtors))
     KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
     KEEP (*(SORT(.dtors.*)))
     KEEP (*crtend.o(.dtors))

     *(.rodata .stub .rodata.* .gnu.linkonce.r.*)

     *(.ARM.extab* .gnu.linkonce.armextab.*)
     *(.gcc_except_table)
     *(.eh_frame_hdr)
     *(.eh_frame)

     *(.init)
     *(.fini)

     PROVIDE_HIDDEN (__preinit_array_start = .);
     KEEP (*(.preinit_array))
     PROVIDE_HIDDEN (__preinit_array_end = .);
     PROVIDE_HIDDEN (__init_array_start = .);
     KEEP (*(SORT(.init_array.*)))
     KEEP (*(.init_array))
     PROVIDE_HIDDEN (__init_array_end = .);
     PROVIDE_HIDDEN (__fini_array_start = .);
     KEEP (*(.fini_array))
     KEEP (*(SORT(.fini_array.*)))
     PROVIDE_HIDDEN (__fini_array_end = .);
} >ROM

/* .ARM.exidx is sorted, so has to go in its own output section.  */
.ARM.exidx : {
     __exidx_start = .;
     *(.ARM.exidx* .gnu.linkonce.armexidx.*)
     __exidx_end = .;
} >ROM
_etext = .;

.data : {
ram_sdata = .;
     __data_load = LOADADDR (.data);
     __data_start = .;
      KEEP(*(.jcr))
     *(.got.plt) *(.got)
     *(.shdata)
PROVIDE(_sdata = . );
     *(.data .stub  .data.* .gnu.linkonce.d.*)
. = ALIGN(4);

     . = ALIGN (4);
PROVIDE(_edata = . );
      _edata = .;
} >RAM AT>ROM

.bss : {
     __bss_start = . ;
     *(.shbss)
     *(.bss .stub .bss.* .gnu.linkonce.b.*)
     *(COMMON)
     . = ALIGN (4);
PROVIDE(_sbss = . );

             *(.bss)

             . = ALIGN(4);

             PROVIDE(_ebss = . );

     __bss_end = .;
} >RAM

PROVIDE(__heap_start__ = __bss_end );
__HEAPSIZE__ = 4097;

I'm presuming it's something to do with my linker script?

I've tried searching for a similar problem, but nothing relevant seems to come up. Would anyone have any suggestions or ideas on what may be the problem?
At this point, even any guesses would be more than welcome!

Thanks!
Parents
  • Note: This was originally posted on 13th January 2011 at http://forums.arm.com


    Had tried that too.
    It doesn't work.

    So arrays over 4 elements don't work. Pointers don't work. Something to do with data allocation...?


    I don't know what the problem is but I'd like to comment on the following - if you want to get help on this forum, please help other people to help you:
    1) Create a minimal example that demonstrates the problem AND compiles correctly;
    2) Do not type the code in, copy & paste the actual code (that compiles)
    3) Ensure the code is formatted for better readability, use {code}{/code} tags around it (square brackets instead of curly), so it looks like below:
    void main()
    {
       char x;
       //some code
       if(x>0)
       {
          //some code
       }
    }


    Then you'll have much better chances of getting help :)

    On your problem now - please do the above and give us the assembly listing of your main().
Reply
  • Note: This was originally posted on 13th January 2011 at http://forums.arm.com


    Had tried that too.
    It doesn't work.

    So arrays over 4 elements don't work. Pointers don't work. Something to do with data allocation...?


    I don't know what the problem is but I'd like to comment on the following - if you want to get help on this forum, please help other people to help you:
    1) Create a minimal example that demonstrates the problem AND compiles correctly;
    2) Do not type the code in, copy & paste the actual code (that compiles)
    3) Ensure the code is formatted for better readability, use {code}{/code} tags around it (square brackets instead of curly), so it looks like below:
    void main()
    {
       char x;
       //some code
       if(x>0)
       {
          //some code
       }
    }


    Then you'll have much better chances of getting help :)

    On your problem now - please do the above and give us the assembly listing of your main().
Children
No data