{"id":2167,"date":"2023-12-07T04:54:25","date_gmt":"2023-12-07T04:54:25","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2167"},"modified":"2023-12-07T04:54:28","modified_gmt":"2023-12-07T04:54:28","slug":"linker-script-change-the-variable-memory-location","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2167","title":{"rendered":"Linker Script: Change the Variable Memory Location"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"525\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/Untitled-1024x525.jpg\" alt=\"\" class=\"wp-image-2169\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/Untitled-1024x525.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/Untitled-300x154.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/Untitled-768x394.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/Untitled-1150x590.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/Untitled-750x385.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/Untitled-400x205.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/Untitled-250x128.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/Untitled.jpg 1258w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this guide, we shall see how to add to linker script command to move the variable location from memory location to another (From RAM_D1 to RAM_D2 in this case on STM32H735 and can be applied to any ARM_GCC).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Memory location before modification.<\/li><li>Memory location after modification.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Memory Location Before the Modification:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>We start from main.c file.<\/p>\n\n\n\n<p>Within the main.c file, include the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stdint.h&quot;\n#include &quot;stdlib.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare an array of size 1000 of uint32_t which will hold 4000 Bytes in the RAM:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint32_t data[1000]<\/pre><\/div>\n\n\n\n<p>Get the size of the unsigned it as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define dataSize\tsizeof(unsigned int)<\/pre><\/div>\n\n\n\n<p>in main code:<\/p>\n\n\n\n<p>fill the array with random numbers to force the compiler to not optimize the array:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">    for(int i=0;i&lt;1000;i++)\n    {\n    \tdata[i]=rand()\/dataSize;\n    }<\/pre><\/div>\n\n\n\n<p>Add the while loop:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\twhile(1)\n\t{\n\n\t}<\/pre><\/div>\n\n\n\n<p>Compile the code now.<\/p>\n\n\n\n<p>After the compilation is done, check the memory region D1 which is the default for STM32H735:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"570\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-1024x570.jpg\" alt=\"\" class=\"wp-image-2170\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-1024x570.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-768x427.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-1536x854.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-2048x1139.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-1150x640.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-750x417.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-400x222.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-19-37-250x139.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>There is 5.85KB of data in the RAM, 3.91 for the array and 1.5 for the stack.<\/p>\n\n\n\n<p>Now, open Memory details, select RAM_D1 then open .bss section:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"570\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-1024x570.jpg\" alt=\"\" class=\"wp-image-2171\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-1024x570.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-768x427.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-1536x854.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-2048x1139.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-1150x640.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-750x417.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-400x222.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-21-14-250x139.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>As you can see, the compiler with the default setup of the linkerscript allocate 3.91KB of data in RAM_D1. <br>Let say, we need to store this buffer into external RAM or another RAM region.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Memory Location After The Modification:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In order to move the variable to another location, we need to create a section, we can simply do this using attribute as following for the buffer:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint32_t data[1000]__attribute__ ((section (&quot;.RAMD2_data&quot;)));<\/pre><\/div>\n\n\n\n<p>We named the section RAMD2_data, by adding this to any other variable, the linkerscript will put the data into RAM_D2 rather than the default D1<\/p>\n\n\n\n<p>Now, open the linkerscript (STM32H735IGKX_FLASH.ld in this case) and add the following line:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"570\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-1024x570.jpg\" alt=\"\" class=\"wp-image-2172\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-1024x570.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-768x427.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-1536x854.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-2048x1139.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-1150x640.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-750x417.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-400x222.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-28-28-250x139.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">.RAMD2_data :\n{\n    KEEP(*(.RAMD2_data))\n} &gt;RAM_D2<\/pre><\/div>\n\n\n\n<p>Save the linkerscript and compile the code and check the memory region, you should see 3.91KB of RAM_D2 has been used as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"570\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-1024x570.jpg\" alt=\"\" class=\"wp-image-2173\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-1024x570.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-768x427.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-1536x854.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-2048x1139.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-1150x640.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-750x417.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-400x222.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-52-00-250x139.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>By checking memory details, we can see the section we created:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"570\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-1024x570.jpg\" alt=\"\" class=\"wp-image-2174\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-1024x570.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-768x427.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-1536x854.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-2048x1139.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-1150x640.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-750x417.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-400x222.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/12\/2023-12-07_07-24-49-250x139.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We have successfully moved the data from RAM_D1 to RAM_D2. The same can be applied to external RAM to hold the variables.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall see how to add to linker script command to move the variable location from memory location to another (From RAM_D1 to RAM_D2 in this case on STM32H735 and can be applied to any ARM_GCC). In this guide, we shall cover the following: Memory location before modification. Memory location after modification. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,12],"tags":[],"class_list":["post-2167","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2167"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2167"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2167\/revisions"}],"predecessor-version":[{"id":2175,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2167\/revisions\/2175"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}