Search code examples
attributesldsectionsiarsymbolicc++

[IAR]how can i printf a section start of end address?


1.I create a custom section for myTest in .icf

define symbol __ICFEDIT_region_TEST_start__ = (0x10080000);
define symbol __ICFEDIT_region_TEST_end__ = (0x100DFFFF);
define region TEST_region  = mem:[from __ICFEDIT_region_TEST_start__ to __ICFEDIT_region_TEST_end__];
place at start of TEST_region {section .test_cases_entries};

2.I code some test in test.c

#pragma section = ".test_cases_entries"
void pfm_test_cases_init(void)
{
  struct pwb_altest_desc *start,*stop;
  start = __section_begin(".test_cases_entries");
  stop = __section_end(".test_cases_entries");
  printf("test section start = %x \n\r",start);
  printf("test section end = %x \n\r",stop);
}
  1. result the response
test section start = 0
test section end = 0
  1. expect result: start of section 0x10080000? end of section 0x100DFFFF?

Solution

  • __section_begin(".section") and __section_end(".section") will be 0 if no part of the section .section is included in the final binary. In your case you first must ensure that the section .test_case_entries is not empty, i.e some module in you project put data in this section. Then you need to make the linker include this data in the final binary. This is either done by making a reference to the data in some module (__section_begin and __section_end does not count), declare the data as __root, or by using --keep on the linker command line.

    An test program that works on my machine is included below. Note that the .icf file is not complete as most of it depends on your target system.

    test.c:

    #include <stdio.h>
    
    // Make the compiler recognize .section as a section for __section_begin and
    // __section_end.
    #pragma section = ".section"
    
    void main(void)
    {
      // Find the start and end address of .section
      int *start = __section_begin(".section");
      int *stop = __section_end(".section");
      printf("section start = %x \n", start);
      printf("section end = %x \n", stop);
    }
    
    // Put data in .section and make the data root to ensure is is included.
    // This can be in a different file.
    #pragma location=".section"
    __root int data[100];
    

    Part of test.icf

    define symbol TEST_start = (0x10080000);
    define symbol TEST_end   = (0x100DFFFF);
    define region TEST_region  = mem:[from TEST_start to TEST_end];
    place at start of TEST_region {section .section};