source: rtems-docs/c-user/linker_sets.rst @ 2ff4255

5
Last change on this file since 2ff4255 was 2ff4255, checked in by Sebastian Huber <sebastian.huber@…>, on 12/06/16 at 11:03:54

Update linker set documentation

Update #2408.
Update #2790.

  • Property mode set to 100644
File size: 21.5 KB
Line 
1.. comment SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. COMMENT: COPYRIGHT (c) 1989-2014.
4.. COMMENT: On-Line Applications Research Corporation (OAR).
5.. COMMENT: All rights reserved.
6
7.. _Linker Sets:
8
9Linker Sets
10***********
11
12.. index:: linkersets
13
14Introduction
15============
16
17Linker sets are a flexible means to create arrays of items out of a set of
18object files at link-time.  For example its possible to define an item *I* of
19type *T* in object file *A* and an item *J* of type *T* in object file *B* to
20be a member of a linker set *S*.  The linker will then collect these two items
21*I* and *J* and place them in consecutive memory locations, so that they can be
22accessed like a normal array defined in one object file.  The size of a linker
23set is defined by its begin and end markers.  A linker set may be empty.  It
24should only contain items of the same type.
25
26The following macros are provided to create, populate and use linker sets.
27
28- RTEMS_LINKER_SET_BEGIN_ - Designator of the linker set begin marker
29
30- RTEMS_LINKER_SET_END_ - Designator of the linker set end marker
31
32- RTEMS_LINKER_SET_SIZE_ - The linker set size in characters
33
34- RTEMS_LINKER_SET_ITEM_COUNT_ - The linker set item count
35
36- RTEMS_LINKER_SET_IS_EMPTY_ - Is the linker set empty?
37
38- RTEMS_LINKER_SET_FOREACH_ - Iterate through the linker set items
39
40- RTEMS_LINKER_ROSET_DECLARE_ - Declares a read-only linker set
41
42- RTEMS_LINKER_ROSET_ - Defines a read-only linker set
43
44- RTEMS_LINKER_ROSET_ITEM_DECLARE_ - Declares a read-only linker set item
45
46- RTEMS_LINKER_ROSET_ITEM_REFERENCE_ - References a read-only linker set item
47
48- RTEMS_LINKER_ROSET_ITEM_ - Defines a read-only linker set item
49
50- RTEMS_LINKER_ROSET_ITEM_ORDERED_ - Defines an ordered read-only linker set item
51
52- RTEMS_LINKER_RWSET_DECLARE_ - Declares a read-write linker set
53
54- RTEMS_LINKER_RWSET_ - Defines a read-write linker set
55
56- RTEMS_LINKER_RWSET_ITEM_DECLARE_ - Declares a read-write linker set item
57
58- RTEMS_LINKER_RWSET_ITEM_REFERENCE_ - References a read-write linker set item
59
60- RTEMS_LINKER_RWSET_ITEM_ - Defines a read-write linker set item
61
62- RTEMS_LINKER_RWSET_ITEM_ORDERED_ - Defines an ordered read-write linker set item
63
64Background
65==========
66
67Linker sets are used not only in RTEMS, but also for example in Linux, in
68FreeBSD, for the GNU C constructor extension and for global C++ constructors.
69They provide a space efficient and flexible means to initialize modules.  A
70linker set consists of
71
72- dedicated input sections for the linker (e.g. ``.ctors`` and ``.ctors.*`` in
73  the case of global constructors),
74
75- a begin marker (e.g. provided by ``crtbegin.o``, and
76
77- an end marker (e.g. provided by ``ctrend.o``).
78
79A module may place a certain data item into the dedicated input section.  The
80linker will collect all such data items in this section and creates a begin and
81end marker.  The initialization code can then use the begin and end markers to
82find all the collected data items (e.g. pointers to initialization functions).
83
84In the linker command file of the GNU linker we need the following output
85section descriptions.
86
87.. code-block:: c
88
89    /* To be placed in a read-only memory region */
90    .rtemsroset : {
91        KEEP (*(SORT(.rtemsroset.*)))
92    }
93    /* To be placed in a read-write memory region */
94    .rtemsrwset : {
95        KEEP (*(SORT(.rtemsrwset.*)))
96    }
97
98The ``KEEP()`` ensures that a garbage collection by the linker will not discard
99the content of this section.  This would normally be the case since the linker
100set items are not referenced directly.  The ``SORT()`` directive sorts the
101input sections lexicographically.  Please note the lexicographical order of the
102``.begin``, ``.content`` and ``.end`` section name parts in the RTEMS linker
103sets macros which ensures that the position of the begin and end markers are
104right.
105
106So, what is the benefit of using linker sets to initialize modules?  It can be
107used to initialize and include only those RTEMS managers and other components
108which are used by the application.  For example, in case an application uses
109message queues, it must call ``rtems_message_queue_create()``.  In the module
110implementing this function, we can place a linker set item and register the
111message queue handler constructor.  Otherwise, in case the application does not
112use message queues, there will be no reference to the
113``rtems_message_queue_create()`` function and the constructor is not
114registered, thus nothing of the message queue handler will be in the final
115executable.
116
117For an example see test program :file:`sptests/splinkersets01`.
118
119Directives
120==========
121
122.. raw:: latex
123
124   \clearpage
125
126.. _RTEMS_LINKER_SET_BEGIN:
127
128RTEMS_LINKER_SET_BEGIN - Designator of the linker set begin marker
129------------------------------------------------------------------
130.. index:: RTEMS_LINKER_SET_BEGIN
131
132CALLING SEQUENCE:
133    .. code-block:: c
134
135        type *begin = RTEMS_LINKER_SET_BEGIN( set );
136
137DESCRIPTION:
138    This macro generates the designator of the begin marker of the linker set
139    identified by ``set``.  The item at the begin marker address is the first
140    member of the linker set if it exists, e.g. the linker set is not empty.  A
141    linker set is empty, if and only if the begin and end markers have the same
142    address.
143
144    The ``set`` parameter itself must be a valid C designator on which no macro
145    expansion is performed.  It uniquely identifies the linker set.
146
147NOTE:
148    The compiler may try to be smart.  In general it will not work to assign linker
149    set begin and end addresses to pointer variables and treat them like
150    ordinary pointers.  The compiler may exploit the fact that actually two
151    distinct objects are involved and use this to optimize.  To avoid trouble
152    use :ref:`RTEMS_LINKER_SET_SIZE`, :ref:`RTEMS_LINKER_SET_ITEM_COUNT`,
153    :ref:`RTEMS_LINKER_SET_IS_EMPTY` and :ref:`RTEMS_LINKER_SET_FOREACH`.
154
155.. raw:: latex
156
157   \clearpage
158
159.. _RTEMS_LINKER_SET_END:
160
161RTEMS_LINKER_SET_END - Designator of the linker set end marker
162--------------------------------------------------------------
163.. index:: RTEMS_LINKER_SET_END
164
165CALLING SEQUENCE:
166    .. code-block:: c
167
168        type *end = RTEMS_LINKER_SET_END( set );
169
170DESCRIPTION:
171    This macro generates the designator of the end marker of the linker set
172    identified by ``set``.  The item at the end marker address is not a member
173    of the linker set.  The ``set`` parameter itself must be a valid C
174    designator on which no macro expansion is performed.  It uniquely
175    identifies the linker set.
176
177.. raw:: latex
178
179   \clearpage
180
181.. _RTEMS_LINKER_SET_SIZE:
182
183RTEMS_LINKER_SET_SIZE - The linker set size in characters
184---------------------------------------------------------
185.. index:: RTEMS_LINKER_SET_SIZE
186
187CALLING SEQUENCE:
188    .. code-block:: c
189
190        size_t size = RTEMS_LINKER_SET_SIZE( set );
191
192DESCRIPTION:
193    This macro returns the size of the linker set identified by ``set`` in
194    characters.  The ``set`` parameter itself must be a valid C designator on
195    which no macro expansion is performed.  It uniquely identifies the linker
196    set.
197
198.. raw:: latex
199
200   \clearpage
201
202.. _RTEMS_LINKER_SET_ITEM_COUNT:
203
204RTEMS_LINKER_SET_ITEM_COUNT - The linker set item count
205---------------------------------------------------------
206.. index:: RTEMS_LINKER_SET_ITEM_COUNT
207
208CALLING SEQUENCE:
209    .. code-block:: c
210
211        size_t item_count = RTEMS_LINKER_SET_ITEM_COUNT( set );
212
213DESCRIPTION:
214    This macro returns the item count of the linker set identified by ``set``.
215    The ``set`` parameter itself must be a valid C designator on which no macro
216    expansion is performed.  It uniquely identifies the linker set.
217
218.. raw:: latex
219
220   \clearpage
221
222.. _RTEMS_LINKER_SET_IS_EMPTY:
223
224RTEMS_LINKER_SET_IS_EMPTY - Is the linker set empty?
225---------------------------------------------------------
226.. index:: RTEMS_LINKER_SET_IS_EMPTY
227
228CALLING SEQUENCE:
229    .. code-block:: c
230
231        bool is_empty = RTEMS_LINKER_SET_IS_EMPTY( set );
232
233DESCRIPTION:
234    This macro returns true if the linker set identified by ``set`` is empty,
235    otherwise returns false.  The ``set`` parameter itself must be a valid C
236    designator on which no macro expansion is performed.  It uniquely
237    identifies the linker set.
238
239.. raw:: latex
240
241   \clearpage
242
243.. _RTEMS_LINKER_SET_FOREACH:
244
245RTEMS_LINKER_SET_FOREACH - Iterate through the linker set items
246---------------------------------------------------------
247.. index:: RTEMS_LINKER_SET_FOREACH
248
249CALLING SEQUENCE:
250    .. code-block:: c
251
252        RTEMS_LINKER_RWSET( myset, int );
253
254        int count( void )
255        {
256          int *item;
257          int n;
258
259          n = 0;
260          RTEMS_LINKER_SET_FOREACH( myset, item ) {
261            n += *item;
262          }
263
264          return n;
265        }
266
267DESCRIPTION:
268    This macro generates a for loop statement which iterates through each item
269    of a linker set identified by ``set``.  The ``set`` parameter itself must
270    be a valid C designator on which no macro expansion is performed.  It
271    uniquely identifies the linker set.  The ``item`` parameter must be a
272    pointer to an item of the linker set.  It iterates through all items of the
273    linker set from begin to end.
274
275.. raw:: latex
276
277   \clearpage
278
279.. _RTEMS_LINKER_ROSET_DECLARE:
280
281RTEMS_LINKER_ROSET_DECLARE - Declares a read-only linker set
282------------------------------------------------------------
283.. index:: RTEMS_LINKER_ROSET_DECLARE
284
285CALLING SEQUENCE:
286    .. code-block:: c
287
288        RTEMS_LINKER_ROSET_DECLARE( set, type );
289
290DESCRIPTION:
291    This macro generates declarations for the begin and end markers of a
292    read-only linker set identified by ``set``.  The ``set`` parameter itself
293    must be a valid C designator on which no macro expansion is performed.  It
294    uniquely identifies the linker set. The ``type`` parameter defines the type
295    of the linker set items.  The type must be the same for all macro
296    invocations of a particular linker set.
297
298.. raw:: latex
299
300   \clearpage
301
302.. _RTEMS_LINKER_ROSET:
303
304RTEMS_LINKER_ROSET - Defines a read-only linker set
305---------------------------------------------------
306.. index:: RTEMS_LINKER_ROSET
307
308CALLING SEQUENCE:
309    .. code-block:: c
310
311        RTEMS_LINKER_ROSET( set, type );
312
313DESCRIPTION:
314    This macro generates definitions for the begin and end markers of a
315    read-only linker set identified by ``set``.  The ``set`` parameter itself
316    must be a valid C designator on which no macro expansion is performed.  It
317    uniquely identifies the linker set. The ``type`` parameter defines the type
318    of the linker set items.  The type must be the same for all macro
319    invocations of a particular linker set.
320
321.. raw:: latex
322
323   \clearpage
324
325.. _RTEMS_LINKER_ROSET_ITEM_DECLARE:
326
327RTEMS_LINKER_ROSET_ITEM_DECLARE - Declares a read-only linker set item
328----------------------------------------------------------------------
329.. index:: RTEMS_LINKER_ROSET_ITEM_DECLARE
330
331CALLING SEQUENCE:
332    .. code-block:: c
333
334        RTEMS_LINKER_ROSET_ITEM_DECLARE( set, type, item );
335
336DESCRIPTION:
337    This macro generates a declaration of an item contained in the read-only
338    linker set identified by ``set``.  The ``set`` parameter itself must be a
339    valid C designator on which no macro expansion is performed.  It uniquely
340    identifies the linker set. The ``type`` parameter defines the type of the
341    linker set items.  The type must be the same for all macro invocations of a
342    particular linker set. The ``item`` parameter itself must be a valid C
343    designator on which no macro expansion is performed.  It uniquely
344    identifies an item in the linker set.
345
346.. raw:: latex
347
348   \clearpage
349
350.. _RTEMS_LINKER_ROSET_ITEM_REFERENCE:
351
352RTEMS_LINKER_ROSET_ITEM_REFERENCE - References a read-only linker set item
353--------------------------------------------------------------------------
354.. index:: RTEMS_LINKER_ROSET_ITEM_REFERENCE
355
356CALLING SEQUENCE:
357    .. code-block:: c
358
359        RTEMS_LINKER_ROSET_ITEM_REFERENCE( set, type, item );
360
361DESCRIPTION:
362    This macro generates a reference to an item contained in the read-only
363    linker set identified by ``set``.  The ``set`` parameter itself must be a
364    valid C designator on which no macro expansion is performed.  It uniquely
365    identifies the linker set. The ``type`` parameter defines the type of the
366    linker set items.  The type must be the same for all macro invocations of a
367    particular linker set. The ``item`` parameter itself must be a valid C
368    designator on which no macro expansion is performed.  It uniquely
369    identifies an item in the linker set.
370
371.. raw:: latex
372
373   \clearpage
374
375.. _RTEMS_LINKER_ROSET_ITEM:
376
377RTEMS_LINKER_ROSET_ITEM - Defines a read-only linker set item
378-------------------------------------------------------------
379.. index:: RTEMS_LINKER_ROSET_ITEM
380
381CALLING SEQUENCE:
382    .. code-block:: c
383
384        RTEMS_LINKER_ROSET_ITEM( set, type, item );
385
386DESCRIPTION:
387    This macro generates a definition of an item contained in the read-only
388    linker set identified by ``set``.  The ``set`` parameter itself must be a
389    valid C designator on which no macro expansion is performed.  It uniquely
390    identifies the linker set. The ``type`` parameter defines the type of the
391    linker set items.  The type must be the same for all macro invocations of a
392    particular linker set. The ``item`` parameter itself must be a valid C
393    designator on which no macro expansion is performed.  It uniquely
394    identifies an item in the linker set.
395
396.. raw:: latex
397
398   \clearpage
399
400.. _RTEMS_LINKER_ROSET_ITEM_ORDERED:
401
402RTEMS_LINKER_ROSET_ITEM_ORDERED - Defines an ordered read-only linker set item
403------------------------------------------------------------------------------
404.. index:: RTEMS_LINKER_ROSET_ITEM_ORDERED
405
406CALLING SEQUENCE:
407    .. code-block:: c
408
409        RTEMS_LINKER_ROSET_ITEM_ORDERED( set, type, item, order );
410
411DESCRIPTION:
412    This macro generates a definition of an ordered item contained in the
413    read-only linker set identified by ``set``.  The ``set`` parameter itself
414    must be a valid C designator on which no macro expansion is performed.  It
415    uniquely identifies the linker set. The ``type`` parameter defines the type
416    of the linker set items.  The type must be the same for all macro
417    invocations of a particular linker set.  The ``item`` parameter itself must
418    be a valid C designator on which no macro expansion is performed.  It
419    uniquely identifies an item in the linker set. The ``order`` parameter must
420    be a valid linker input section name part on which macro expansion is
421    performed.  The items are lexicographically ordered according to the
422    ``order`` parameter within a linker set.  Ordered items are placed before
423    unordered items in the linker set.
424
425NOTES:
426    To be resilient to typos in the order parameter, it is recommended to use
427    the following construct in macros defining items for a particular linker
428    set (see enum in ``XYZ_ITEM()``).
429
430    .. code-block:: c
431
432        #include <rtems/linkersets.h>
433
434        typedef struct {
435            int foo;
436        } xyz_item;
437
438        /* The XYZ-order defines */
439        #define XYZ_ORDER_FIRST 0x00001000
440        #define XYZ_ORDER_AND_SO_ON 0x00002000
441
442        /* Defines an ordered XYZ-item */
443        #define XYZ_ITEM( item, order ) \
444                    enum { xyz_##item = order - order }; \
445                    RTEMS_LINKER_ROSET_ITEM_ORDERED( \
446                        xyz, const xyz_item *, item, order \
447                    ) = { &item }
448
449        /* Example item */
450        static const xyz_item some_item = { 123 };
451        XYZ_ITEM( some_item, XYZ_ORDER_FIRST );
452
453.. raw:: latex
454
455   \clearpage
456
457.. _RTEMS_LINKER_RWSET_DECLARE:
458
459RTEMS_LINKER_RWSET_DECLARE - Declares a read-write linker set
460-------------------------------------------------------------
461.. index:: RTEMS_LINKER_RWSET_DECLARE
462
463CALLING SEQUENCE:
464    .. code-block:: c
465
466        RTEMS_LINKER_RWSET_DECLARE( set, type );
467
468DESCRIPTION:
469    This macro generates declarations for the begin and end markers of a
470    read-write linker set identified by ``set``.  The ``set`` parameter itself
471    must be a valid C designator on which no macro expansion is performed.  It
472    uniquely identifies the linker set. The ``type`` parameter defines the type
473    of the linker set items.  The type must be the same for all macro
474    invocations of a particular linker set.
475
476.. raw:: latex
477
478   \clearpage
479
480.. _RTEMS_LINKER_RWSET:
481
482RTEMS_LINKER_RWSET - Defines a read-write linker set
483----------------------------------------------------
484.. index:: RTEMS_LINKER_RWSET
485
486CALLING SEQUENCE:
487    .. code-block:: c
488
489        RTEMS_LINKER_RWSET( set, type );
490
491DESCRIPTION:
492    This macro generates definitions for the begin and end markers of a
493    read-write linker set identified by ``set``.  The ``set`` parameter itself
494    must be a valid C designator on which no macro expansion is performed.  It
495    uniquely identifies the linker set. The ``type`` parameter defines the type
496    of the linker set items.  The type must be the same for all macro
497    invocations of a particular linker set.
498
499.. raw:: latex
500
501   \clearpage
502
503.. _RTEMS_LINKER_RWSET_ITEM_DECLARE:
504
505RTEMS_LINKER_RWSET_ITEM_DECLARE - Declares a read-write linker set item
506-----------------------------------------------------------------------
507.. index:: RTEMS_LINKER_RWSET_ITEM_DECLARE
508
509CALLING SEQUENCE:
510    .. code-block:: c
511
512        RTEMS_LINKER_RWSET_ITEM_DECLARE( set, type, item );
513
514DESCRIPTION:
515    This macro generates a declaration of an item contained in the read-write
516    linker set identified by ``set``.  The ``set`` parameter itself must be a
517    valid C designator on which no macro expansion is performed.  It uniquely
518    identifies the linker set. The ``type`` parameter defines the type of the
519    linker set items.  The type must be the same for all macro invocations of a
520    particular linker set. The ``item`` parameter itself must be a valid C
521    designator on which no macro expansion is performed.  It uniquely
522    identifies an item in the linker set.
523
524.. raw:: latex
525
526   \clearpage
527
528.. _RTEMS_LINKER_RWSET_ITEM_REFERENCE:
529
530RTEMS_LINKER_RWSET_ITEM_REFERENCE - References a read-write linker set item
531---------------------------------------------------------------------------
532.. index:: RTEMS_LINKER_RWSET_ITEM_REFERENCE
533
534CALLING SEQUENCE:
535    .. code-block:: c
536
537        RTEMS_LINKER_RWSET_ITEM_REFERENCE( set, type, item );
538
539DESCRIPTION:
540    This macro generates a reference to an item contained in the read-write
541    linker set identified by ``set``.  The ``set`` parameter itself must be a
542    valid C designator on which no macro expansion is performed.  It uniquely
543    identifies the linker set. The ``type`` parameter defines the type of the
544    linker set items.  The type must be the same for all macro invocations of a
545    particular linker set. The ``item`` parameter itself must be a valid C
546    designator on which no macro expansion is performed.  It uniquely
547    identifies an item in the linker set.
548
549.. raw:: latex
550
551   \clearpage
552
553.. _RTEMS_LINKER_RWSET_ITEM:
554
555RTEMS_LINKER_RWSET_ITEM - Defines a read-write linker set item
556--------------------------------------------------------------
557.. index:: RTEMS_LINKER_RWSET_ITEM
558
559CALLING SEQUENCE:
560    .. code-block:: c
561
562        RTEMS_LINKER_RWSET_ITEM( set, type, item );
563
564DESCRIPTION:
565    This macro generates a definition of an item contained in the read-write
566    linker set identified by ``set``.  The ``set`` parameter itself must be a
567    valid C designator on which no macro expansion is performed.  It uniquely
568    identifies the linker set. The ``type`` parameter defines the type of the
569    linker set items.  The type must be the same for all macro invocations of a
570    particular linker set. The ``item`` parameter itself must be a valid C
571    designator on which no macro expansion is performed.  It uniquely
572    identifies an item in the linker set.
573
574.. raw:: latex
575
576   \clearpage
577
578.. _RTEMS_LINKER_RWSET_ITEM_ORDERED:
579
580RTEMS_LINKER_RWSET_ITEM_ORDERED - Defines an ordered read-write linker set item
581-------------------------------------------------------------------------------
582.. index:: RTEMS_LINKER_RWSET_ITEM_ORDERED
583
584CALLING SEQUENCE:
585    .. code-block:: c
586
587        RTEMS_LINKER_RWSET_ITEM_ORDERED( set, type, item, order );
588
589DESCRIPTION:
590    This macro generates a definition of an ordered item contained in the
591    read-write linker set identified by ``set``.  The ``set`` parameter itself
592    must be a valid C designator on which no macro expansion is performed.  It
593    uniquely identifies the linker set. The ``type`` parameter defines the type
594    of the linker set items.  The type must be the same for all macro
595    invocations of a particular linker set.  The ``item`` parameter itself must
596    be a valid C designator on which no macro expansion is performed.  It
597    uniquely identifies an item in the linker set. The ``order`` parameter must
598    be a valid linker input section name part on which macro expansion is
599    performed.  The items are lexicographically ordered according to the
600    ``order`` parameter within a linker set.  Ordered items are placed before
601    unordered items in the linker set.
602
603NOTES:
604    To be resilient to typos in the order parameter, it is recommended to use
605    the following construct in macros defining items for a particular linker
606    set (see enum in ``XYZ_ITEM()``).
607
608    .. code-block:: c
609
610        #include <rtems/linkersets.h>
611
612        typedef struct {
613            int foo;
614        } xyz_item;
615
616        /* The XYZ-order defines */
617        #define XYZ_ORDER_FIRST 0x00001000
618        #define XYZ_ORDER_AND_SO_ON 0x00002000
619
620        /* Defines an ordered XYZ-item */
621        #define XYZ_ITEM( item, order ) \
622                    enum { xyz_##item = order - order }; \
623                    RTEMS_LINKER_RWSET_ITEM_ORDERED( \
624                        xyz, const xyz_item *, item, order \
625                    ) = { &item }
626        /* Example item */
627        static const xyz_item some_item = { 123 };
628        XYZ_ITEM( some_item, XYZ_ORDER_FIRST );
Note: See TracBrowser for help on using the repository browser.