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!
  • Note: This was originally posted on 12th January 2011 at http://forums.arm.com


    At this point, even any guesses would be more than welcome!

    Thanks!


    You asked for guesses: the problem might have nothing to do with the array size, etc., your uart... function might be failing sometimes for unknown reason and you are just looking for a problem in a wrong place.
    FWIW.
  • Note: This was originally posted on 12th January 2011 at http://forums.arm.com


    You asked for guesses: the problem might have nothing to do with the array size, etc., your uart... function might be failing sometimes for unknown reason and you are just looking for a problem in a wrong place.
    FWIW.


    Thanks for the response!
    However, I thought that as well. I'm not really interested in seeing the characters stored, I want it to be decyphered within the code, so I had written another bit of code to verify the value held :

    Code 1:
    char chartest[10]={"HELLO"}; //testing an array
    char *chartest1 = "HELLO"; //test a pointer
    char *ptr = &chartest1; //initialise pointer
    uart_send_num(*ptr);

    if(chartest[1]==69) //ASCII E=69
    {
    uart_send('Y');
    }

    if(chartest[1]=='E')
    {
    uart_send('Y');
    }

    ptr=strchr(chartest,'H'); //find first occurance of H
    uart_send('.');//separator
    uart_send_num(*ptr); //output 1st occurance of H

    ptr=strchr(chartest,'E');
    uart_send('.'); //separator
    uart_send_num(*ptr); //output 1st occurance of E

    Result 1:
    0000,.00126.00126.0000,.0000,

    This should have output Y if atleast the value was being stored correctly, but the uart isn't outputtin the data correctly.
    if(chartest[1]=='E')
    {
    uart_send('Y');
    }
    However, I change chartest to hold 4 elements as follows:
    char chartest[4]="HEL";

    And rerun the code I get:
    0001.YY.00126.00126.00000.00000

    Any more ideas I could attempt?
  • Note: This was originally posted on 12th January 2011 at http://forums.arm.com


    Just tried another way. It works beyond 4 elements if I use single quotes:

    Code 1:

    char chartest2[12]={'H','E','L','L','O',' ','W','O','R','L','D'};
    [snip]
    Result 1:
    HELLO WORLD

    This problem is confusing me even more!


    I would also try
    const char chartest[5]="HELL";

    to see what happens.
  • Note: This was originally posted on 12th January 2011 at http://forums.arm.com


    I would also try
    const char chartest[5]="HELL";

    to see what happens.


    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...?
  • 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().