linker-script-generation.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. Linker Script Generation
  2. ========================
  3. Overview
  4. --------
  5. There are several :ref:`memory regions<memory-layout>` where code and data can be placed. Usually, code and read-only data are placed in flash regions,
  6. writable data in RAM, etc. A common action is changing where code/data are mapped by default, say placing critical code/rodata in RAM for performance
  7. reasons or placing code/data/rodata in RTC memory for use in a wake stub or the ULP coprocessor.
  8. IDF provides the ability for defining these placements at the component level using the linker script generation mechanism. The component presents
  9. how it would like to map the input sections of its object files (or even functions/data) through :ref:`linker fragment files<ldgen-fragment-files>`. During app build,
  10. the linker fragment files are collected, parsed and processed; and the :ref:`linker script template<ldgen-script-templates>` is augmented with
  11. information generated from the fragment files to produce the final linker script. This linker script is then used for the linking
  12. the final app binary.
  13. Quick Start
  14. ------------
  15. This section presents a guide for quickly placing code/data to RAM and RTC memory; as well as demonstrating how to make these placements
  16. dependent on project configuration values. In a true quick start fashion, this section glosses over terms and concepts that will be discussed
  17. at a later part of the document. However, whenever it does so, it provides a link to the relevant section on the first mention.
  18. .. _ldgen-add-fragment-file :
  19. Preparation
  20. ^^^^^^^^^^^
  21. Make
  22. """"
  23. Create a linker fragment file inside the component directory, which is just a text file with a .lf extension. In order for the build system to collect your fragment file,
  24. add an entry to it from the component, set the variable ``COMPONENT_ADD_LDFRAGMENTS`` to your linker file/s before the ``register_component`` call.
  25. .. code-block:: make
  26. # file paths relative to component Makefile
  27. COMPONENT_ADD_LDFRAGMENTS += "path/to/linker_fragment_file.lf" "path/to/another_linker_fragment_file.lf"
  28. CMake
  29. """""
  30. For CMake set the variable ``COMPONENT_ADD_LDFRAGMENTS`` to your linker file/s before the ``register_component`` call.
  31. .. code-block:: cmake
  32. # file paths relative to CMakeLists.txt
  33. set(COMPONENT_ADD_LDFRAGMENTS "path/to/linker_fragment_file.lf" "path/to/another_linker_fragment_file.lf")
  34. register_component()
  35. Specifying placements
  36. ^^^^^^^^^^^^^^^^^^^^^
  37. This mechanism allows specifying placement of the following entities:
  38. - one or multiple object files within the component
  39. - one or multiple function/variable using their names
  40. - the entire component library
  41. For the following text, suppose we have the following:
  42. - a component named ``component`` that is archived as library ``libcomponent.a`` during build
  43. - three object files archived under the library, ``object1.o``, ``object2.o`` and ``object3.o``
  44. - under ``object1.o``, the function ``function1`` is defined; under ``object2.o``, the function ``function2`` is defined
  45. - there exists configuration ``PERFORMANCE_MODE`` and ``PERFORMANCE_LEVEL`` in one of the IDF KConfig files, with the set value indicated by entries ``CONFIG_PERFORMANCE_MODE`` and ``CONFIG_PERFORMANCE_LEVEL`` in the project sdkconfig
  46. In the created linker fragment file, we write:
  47. .. code-block:: none
  48. [mapping]
  49. archive: libcomponent.a
  50. entries:
  51. This creates an empty :ref:`mapping fragment<ldgen-mapping-fragment>`, which doesn't do anything yet. During linking the :ref:`default placements<ldgen-default-placements>`
  52. will still be used for ``libcomponent.a``, unless the ``entries`` key is populated.
  53. .. _ldgen-placing-object-files :
  54. Placing object files
  55. """"""""""""""""""""
  56. Suppose the entirety of ``object1.o`` is performance-critical, so it is desirable to place it in RAM. On the other hand, all of ``object2.o``
  57. contains things to be executed coming out of deep sleep, so it needs to be put under RTC memory. We can write:
  58. .. code-block:: none
  59. [mapping]
  60. archive: libcomponent.a
  61. entries:
  62. object1 (noflash) # places all code / read-only data under IRAM/ DRAM
  63. object2 (rtc) # places all code/ data and read-only data under RTC fast memory/ RTC slow memory
  64. What happens to ``object3.o``? Since it is not specified, default placements are used for ``object3.o``.
  65. Placing functions/data using their names
  66. """"""""""""""""""""""""""""""""""""""""
  67. Continuing our example, suppose that among functions defined under ``object1.o``, only ``function1`` is performance-critical; and under ``object2.o``,
  68. only ``function2`` needs to execute after the chip comes out of deep sleep. This could be accomplished by writing:
  69. .. code-block:: none
  70. [mapping]
  71. archive: libcomponent.a
  72. entries:
  73. object1:function1 (noflash)
  74. object2:function2 (rtc)
  75. The default placements are used for the rest of the functions in ``object1.o`` and ``object2.o`` and the entire ``object3.o``. Something similar
  76. can be achieved for placing data by writing the variable name instead of the function name after ``:``.
  77. .. warning::
  78. There are :ref:`limitations<ldgen-type3-limitations>` in placing code/data using their symbol names. In order to ensure proper placements, an alternative would be to group
  79. relevant code and data into source files, and :ref:`use object file placement<ldgen-placing-object-files>`.
  80. Placing entire component
  81. """"""""""""""""""""""""
  82. In this example, suppose that the entire component needs to be placed in RAM. This can be written as:
  83. .. code-block:: none
  84. [mapping]
  85. archive: libcomponent.a
  86. entries:
  87. * (noflash)
  88. Similarly, this places the entire component in RTC memory:
  89. .. code-block:: none
  90. [mapping]
  91. archive: libcomponent.a
  92. entries:
  93. * (rtc)
  94. Configuration-dependent placements
  95. """"""""""""""""""""""""""""""""""
  96. Suppose that the entire component library should only be placed when ``CONFIG_PERFORMANCE_MODE == y`` in the sdkconfig. This could be written as:
  97. .. code-block:: none
  98. [mapping]
  99. archive: libcomponent.a
  100. entries:
  101. : PERFORMANCE_MODE = y
  102. * (noflash)
  103. In pseudocode, this translates to:
  104. .. code-block:: none
  105. if PERFORMANCE_MODE = y
  106. place entire libcomponent.a in RAM
  107. else
  108. use default placements
  109. It is also possible to have multiple conditions to test. Suppose the following requirements: when ``CONFIG_PERFORMANCE_LEVEL == 1``, only ``object1.o`` is put in RAM;
  110. when ``CONFIG_PERFORMANCE_LEVEL == 2``, ``object1.o`` and ``object2.o``; and when ``CONFIG_PERFORMANCE_LEVEL == 3`` all object files under the archive
  111. are to be put into RAM. When these three are false however, put entire library in RTC memory. This scenario is a bit contrived, but,
  112. it can be written as:
  113. .. code-block:: none
  114. [mapping]
  115. archive: libcomponent.a
  116. entries:
  117. : PERFORMANCE_LEVEL = 3
  118. * (noflash)
  119. : PERFORMANCE_LEVEL = 2
  120. object1 (noflash)
  121. object2 (noflash)
  122. : PERFORMANCE_LEVEL = 1
  123. object1 (noflash)
  124. : default
  125. * (rtc)
  126. Which reads:
  127. .. code-block:: none
  128. if CONFIG_PERFORMANCE_LEVEL == 3
  129. place entire libcomponent.a in RAM
  130. else if CONFIG_PERFORMANCE_LEVEL == 2
  131. only place object1.o and object2.o in RAM
  132. else if CONFIG_PERFORMANCE_LEVEL == 1
  133. only place object1.o in RAM
  134. else
  135. place entire libcomponent.a in RTC memory
  136. The conditions test :ref:`support other operations<ldgen-condition-entries>`.
  137. .. _ldgen-default-placements:
  138. The 'default' placements
  139. ^^^^^^^^^^^^^^^^^^^^^^^^
  140. Up until this point, the term 'default placements' has been mentioned as fallback placements for when the
  141. placement rules ``rtc`` and ``noflash`` are not specified. The tokens ``noflash`` or ``rtc`` are not merely keywords known by the mechanism, but are actually
  142. objects called :ref:`scheme fragments<ldgen-scheme-fragment>` that are specified by the user. Due to the commonness of these placement use cases,
  143. they are pre-defined in IDF.
  144. Similarly, there exists a ``default`` scheme fragment which defines what the default placement rules should be, which is discussed :ref:`here<ldgen-default-scheme>`.
  145. .. note::
  146. For an example of an IDF component using this feature, see :component_file:`freertos/CMakeLists.txt`. The ``freertos`` component uses this
  147. mechanism to place all code, literal and rodata of all of its object files to the instruction RAM memory region for performance reasons.
  148. This marks the end of the quick start guide. The following text discusses this mechanism in a little bit more detail, such its components, essential concepts,
  149. the syntax, how it is integrated with the build system, etc. The following sections should be helpful in creating custom mappings or modifying default
  150. behavior.
  151. Components
  152. ----------
  153. .. _ldgen-fragment-files :
  154. Linker Fragment Files
  155. ^^^^^^^^^^^^^^^^^^^^^
  156. The fragment files contain objects called 'fragments'. These fragments contain pieces of information which, when put together, form
  157. placement rules that tell where to place sections of object files in the output binary.
  158. Another way of putting it is that processing linker fragment files aims to create the section placement rules inside GNU LD ``SECTIONS`` command.
  159. Where to collect and put these section placement rules is represented internally as a ``target`` token.
  160. The three types of fragments are discussed below.
  161. .. note::
  162. Fragments have a name property (except mapping fragments) and are known globally.
  163. Fragment naming follows C variable naming rules, i.e. case sensitive, must begin with a letter or underscore, alphanumeric/underscore after
  164. initial characters are allowed, no spaces/special characters. Each type of fragment has its own namespace. In cases where multiple fragments
  165. of the same type and name are encountered, an exception is thrown.
  166. .. _ldgen-sections-fragment :
  167. I. Sections
  168. """""""""""
  169. Sections fragments defines a list of object file sections that the GCC compiler emits. It may be a default section (e.g. ``.text``, ``.data``) or
  170. it may be user defined section through the ``__attribute__`` keyword.
  171. The use of an optional '+' indicates the inclusion of the section in the list, as well as sections that start with it. This is the preferred method over listing both explicitly.
  172. **Syntax**
  173. .. code-block:: none
  174. [sections:name]
  175. entries:
  176. .section+
  177. .section
  178. ...
  179. **Example**
  180. .. code-block:: none
  181. # Non-preferred
  182. [sections:text]
  183. entries:
  184. .text
  185. .text.*
  186. .literal
  187. .literal.*
  188. # Preferred, equivalent to the one above
  189. [sections:text]
  190. entries:
  191. .text+ # means .text and .text.*
  192. .literal+ # means .literal and .literal.*
  193. .. _ldgen-scheme-fragment :
  194. II. Scheme
  195. """"""""""
  196. Scheme fragments define what ``target`` a sections fragment is assigned to.
  197. **Syntax**
  198. .. code-block:: none
  199. [scheme:name]
  200. entries:
  201. sections -> target
  202. sections -> target
  203. ...
  204. **Example**
  205. .. code-block:: none
  206. [scheme:noflash]
  207. entries:
  208. text -> iram0_text # the entries under the sections fragment named text will go to iram0_text
  209. rodata -> dram0_data # the entries under the sections fragment named rodata will go to dram0_data
  210. .. _ldgen-default-scheme:
  211. **The** ``default`` **scheme**
  212. There exists a special scheme with the name ``default``. This scheme is special because catch-all placement rules are generated from
  213. its entries. This means that, if one of its entries is ``text -> flash_text``, the placement rule
  214. .. code-block:: none
  215. *(.literal .literal.* .text .text.*)
  216. will be generated for the target ``flash_text``.
  217. These catch-all rules then effectively serve as fallback rules for those whose mappings were not specified.
  218. .. note::
  219. The ``default scheme`` is defined in :component:`esp32/ld/esp32_fragments.lf`. The ``noflash`` and ``rtc`` scheme fragments which are
  220. built-in schemes referenced in the quick start guide are also defined in this file.
  221. .. _ldgen-mapping-fragment :
  222. III. Mapping
  223. """"""""""""
  224. Mapping fragments define what scheme fragment to use for mappable entities, i.e. object files, function names, variable names. There are two types of entries
  225. for this fragment: mapping entries and condition entries.
  226. .. note::
  227. Mapping fragments have no explicit name property. Internally, the name is constructed from the value of the archive entry.
  228. **Syntax**
  229. .. code-block:: none
  230. [mapping]
  231. archive: archive # output archive file name, as built (i.e. libxxx.a)
  232. entries:
  233. : condition # condition entry, non-default
  234. object:symbol (scheme) # mapping entry, Type I
  235. object (scheme) # mapping entry, Type II
  236. * (scheme) # mapping entry, Type III
  237. # optional separation/comments, for readability
  238. : default # condition entry, default
  239. * (scheme) # mapping entry, Type III
  240. .. _ldgen-mapping-entries :
  241. **Mapping Entries**
  242. There are three types of mapping entries:
  243. ``Type I``
  244. The object file name and symbol name are specified. The symbol name can be a function name or a variable name.
  245. ``Type II``
  246. Only the object file name is specified.
  247. ``Type III``
  248. ``*`` is specified, which is a short-hand for all the object files under the archive.
  249. To know what a mapping entry means, let us expand a ``Type II`` entry. Originally:
  250. .. code-block:: none
  251. object (scheme)
  252. Then expanding the scheme fragment from its entries definitions, we have:
  253. .. code-block:: none
  254. object (sections -> target,
  255. sections -> target,
  256. ...)
  257. Expanding the sections fragment with its entries definition:
  258. .. code-block:: none
  259. object (.section, # given this object file
  260. .section, # put its sections listed here at this
  261. ... -> target, # target
  262. .section,
  263. .section, # same should be done for these sections
  264. ... -> target,
  265. ...) # and so on
  266. .. _ldgen-type3-limitations :
  267. **On** ``Type I`` **Mapping Entries**
  268. ``Type I`` mapping entry is possible due to compiler flags ``-ffunction-sections`` and ``-ffdata-sections``. If the user opts to remove these flags, then
  269. the ``Type I`` mapping will not work. Furthermore, even if the user does not opt to compile without these flags, there are still limitations
  270. as the implementation is dependent on the emitted output sections.
  271. For example, with ``-ffunction-sections``, separate sections are emitted for each function; with section names predictably constructed i.e. ``.text.{func_name}``
  272. and ``.literal.{func_name}``. This is not the case for string literals within the function, as they go to pooled or generated section names.
  273. With ``-fdata-sections``, for global scope data the compiler predictably emits either ``.data.{var_name}``, ``.rodata.{var_name}`` or ``.bss.{var_name}``; and so ``Type I`` mapping entry works for these.
  274. However, this is not the case for static data declared in function scope, as the generated section name is a result of mangling the variable name with some other information.
  275. .. _ldgen-condition-entries :
  276. **Condition Entries**
  277. Condition entries enable the linker script generation to be configuration-aware. Depending on whether expressions involving configuration values
  278. are true or not, a particular set of mapping entries can be used. The evaluation uses ``eval_string`` from ``:idf_file:`tools/kconfig_new/kconfiglib.py``` and adheres to its required syntax and limitations.
  279. All mapping entries defined after a condition entry until the next one or the end of the mapping fragment belongs to that condition entry. During processing
  280. conditions are tested sequentially, and the mapping entries under the first condition that evaluates to ``TRUE`` are used.
  281. A default condition can be defined (though every mapping contains an implicit, empty one), whose mapping entries get used in the event no conditions evaluates to ``TRUE``.
  282. **Example**
  283. .. code-block:: none
  284. [scheme:noflash]
  285. entries:
  286. text -> iram0_text
  287. rodata -> dram0_data
  288. [mapping:lwip]
  289. archive: liblwip.a
  290. entries:
  291. : LWIP_IRAM_OPTIMIZATION = y # if CONFIG_LWIP_IRAM_OPTIMIZATION is set to 'y' in sdkconfig
  292. ip4:ip4_route_src_hook (noflash) # map ip4.o:ip4_route_src_hook, ip4.o:ip4_route_src and
  293. ip4:ip4_route_src (noflash) # ip4.o:ip4_route using the noflash scheme, which puts
  294. ip4:ip4_route (noflash) # them in RAM
  295. : default # else no special mapping rules apply
  296. .. _ldgen-script-templates :
  297. Linker Script Template
  298. ^^^^^^^^^^^^^^^^^^^^^^
  299. The linker script template is the skeleton in which the generated placement rules are put into. It is an otherwise ordinary linker script, with a specific marker syntax
  300. that indicates where the generated placement rules are placed.
  301. **Syntax**
  302. To reference the placement rules collected under a ``target`` token, the following syntax is used:
  303. .. code-block:: none
  304. mapping[target]
  305. **Example**
  306. The example below is an excerpt from a possible linker script template. It defines an output section ``.iram0.text``, and inside is a marker referencing
  307. the target ``iram0_text``.
  308. .. code-block:: none
  309. .iram0.text :
  310. {
  311. /* Code marked as runnning out of IRAM */
  312. _iram_text_start = ABSOLUTE(.);
  313. /* Marker referencing iram0_text */
  314. mapping[iram0_text]
  315. INCLUDE esp32.spiram.rom-functions-iram.ld
  316. _iram_text_end = ABSOLUTE(.);
  317. } > iram0_0_seg
  318. Suppose the generator collected the fragment definitions below:
  319. .. code-block:: none
  320. [sections:text]
  321. .text+
  322. .literal+
  323. [sections:iram]
  324. .iram1+
  325. [scheme:default]
  326. entries:
  327. text -> flash_text
  328. iram -> iram0_text
  329. [scheme:noflash]
  330. entries:
  331. text -> iram0_text
  332. [mapping:freertos]
  333. archive: libfreertos.a
  334. entries:
  335. * (noflash)
  336. Then the corresponding excerpt from the generated linker script will be as follows:
  337. .. code-block:: c
  338. .iram0.text :
  339. {
  340. /* Code marked as runnning out of IRAM */
  341. _iram_text_start = ABSOLUTE(.);
  342. /* Placement rules generated from the processed fragments, placed where the marker was in the template */
  343. *(.iram1 .iram1.*)
  344. *libfreertos.a:(.literal .text .literal.* .text.*)
  345. INCLUDE esp32.spiram.rom-functions-iram.ld
  346. _iram_text_end = ABSOLUTE(.);
  347. } > iram0_0_seg
  348. ``*libfreertos.a:(.literal .text .literal.* .text.*)``
  349. Rule generated from the entry ``* (noflash)`` of the ``freertos`` mapping fragment. All ``text`` sections of all
  350. object files under the archive ``libfreertos.a`` will be collected under the target ``iram0_text`` (as per the ``noflash`` scheme)
  351. and placed wherever in the template ``iram0_text`` is referenced by a marker.
  352. ``*(.iram1 .iram1.*)``
  353. Rule generated from the default scheme entry ``iram -> iram0_text``. Since the default scheme specifies an ``iram -> iram0_text`` entry,
  354. it too is placed wherever ``iram0_text`` is referenced by a marker. Since it is a rule generated from the default scheme, it comes first
  355. among all other rules collected under the same target name.
  356. Integration with Build System
  357. -----------------------------
  358. The linker script generation occurs during application build, before the final output binary is linked. The tool that implements the mechanism
  359. lives under ``$(IDF_PATH)/tools/ldgen``.
  360. Linker Script Template
  361. ^^^^^^^^^^^^^^^^^^^^^^
  362. Currently, the linker script template used is :component:`esp32/ld/esp32.common.ld.in`, and is used only for the app build. The generated output script is
  363. put under the build directory of the same component. Modifying this linker script template triggers a re-link of the app binary.
  364. Linker Fragment File
  365. ^^^^^^^^^^^^^^^^^^^^
  366. Any component can add a fragment file to the build. In order to add a fragment file to process, use the command ``ldgen_add_fragment_file`` as mentioned :ref:`here<ldgen-add-fragment-file>`.
  367. Modifying any fragment file presented to the build system triggers a re-link of the app binary.