source: rtems-docs/c-user/linker_sets.rst @ 4da4a15

4.115
Last change on this file since 4da4a15 was 4da4a15, checked in by Chris Johns <chrisj@…>, on 11/09/16 at 00:42:10

c-user: Fix header levels. Minor fixes.

  • Property mode set to 100644
File size: 18.9 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_ROSET_DECLARE_ - Declares a read-only linker set
35
36- RTEMS_LINKER_ROSET_ - Defines a read-only linker set
37
38- RTEMS_LINKER_ROSET_ITEM_DECLARE_ - Declares a read-only linker set item
39
40- RTEMS_LINKER_ROSET_ITEM_REFERENCE_ - References a read-only linker set item
41
42- RTEMS_LINKER_ROSET_ITEM_ - Defines a read-only linker set item
43
44- RTEMS_LINKER_ROSET_ITEM_ORDERED_ - Defines an ordered read-only linker set item
45
46- RTEMS_LINKER_RWSET_DECLARE_ - Declares a read-write linker set
47
48- RTEMS_LINKER_RWSET_ - Defines a read-write linker set
49
50- RTEMS_LINKER_RWSET_ITEM_DECLARE_ - Declares a read-write linker set item
51
52- RTEMS_LINKER_RWSET_ITEM_REFERENCE_ - References a read-write linker set item
53
54- RTEMS_LINKER_RWSET_ITEM_ - Defines a read-write linker set item
55
56- RTEMS_LINKER_RWSET_ITEM_ORDERED_ - Defines an ordered read-write linker set item
57
58Background
59==========
60
61Linker sets are used not only in RTEMS, but also for example in Linux, in
62FreeBSD, for the GNU C constructor extension and for global C++ constructors.
63They provide a space efficient and flexible means to initialize modules.  A
64linker set consists of
65
66- dedicated input sections for the linker (e.g. ``.ctors`` and ``.ctors.*`` in
67  the case of global constructors),
68
69- a begin marker (e.g. provided by ``crtbegin.o``, and
70
71- an end marker (e.g. provided by ``ctrend.o``).
72
73A module may place a certain data item into the dedicated input section.  The
74linker will collect all such data items in this section and creates a begin and
75end marker.  The initialization code can then use the begin and end markers to
76find all the collected data items (e.g. pointers to initialization functions).
77
78In the linker command file of the GNU linker we need the following output
79section descriptions.
80
81.. code-block:: c
82
83    /* To be placed in a read-only memory region */
84    .rtemsroset : {
85        KEEP (*(SORT(.rtemsroset.*)))
86    }
87    /* To be placed in a read-write memory region */
88    .rtemsrwset : {
89        KEEP (*(SORT(.rtemsrwset.*)))
90    }
91
92The ``KEEP()`` ensures that a garbage collection by the linker will not discard
93the content of this section.  This would normally be the case since the linker
94set items are not referenced directly.  The ``SORT()`` directive sorts the
95input sections lexicographically.  Please note the lexicographical order of the
96``.begin``, ``.content`` and ``.end`` section name parts in the RTEMS linker
97sets macros which ensures that the position of the begin and end markers are
98right.
99
100So, what is the benefit of using linker sets to initialize modules?  It can be
101used to initialize and include only those RTEMS managers and other components
102which are used by the application.  For example, in case an application uses
103message queues, it must call ``rtems_message_queue_create()``.  In the module
104implementing this function, we can place a linker set item and register the
105message queue handler constructor.  Otherwise, in case the application does not
106use message queues, there will be no reference to the
107``rtems_message_queue_create()`` function and the constructor is not
108registered, thus nothing of the message queue handler will be in the final
109executable.
110
111For an example see test program :file:`sptests/splinkersets01`.
112
113Directives
114==========
115
116.. raw:: latex
117
118   \clearpage
119
120.. _RTEMS_LINKER_SET_BEGIN:
121
122RTEMS_LINKER_SET_BEGIN - Designator of the linker set begin marker
123------------------------------------------------------------------
124.. index:: RTEMS_LINKER_SET_BEGIN
125
126CALLING SEQUENCE:
127    .. code-block:: c
128
129        volatile type *begin = RTEMS_LINKER_SET_BEGIN( set );
130
131DESCRIPTION:
132    This macro generates the designator of the begin marker of the linker set
133    identified by ``set``.  The item at the begin marker address is the first
134    member of the linker set if it exists, e.g. the linker set is not empty.  A
135    linker set is empty, if and only if the begin and end markers have the same
136    address.
137
138    The ``set`` parameter itself must be a valid C designator on which no macro
139    expansion is performed.  It uniquely identifies the linker set.
140
141.. raw:: latex
142
143   \clearpage
144
145.. _RTEMS_LINKER_SET_END:
146
147RTEMS_LINKER_SET_END - Designator of the linker set end marker
148--------------------------------------------------------------
149.. index:: RTEMS_LINKER_SET_END
150
151CALLING SEQUENCE:
152    .. code-block:: c
153
154        volatile type *end = RTEMS_LINKER_SET_END( set );
155
156DESCRIPTION:
157    This macro generates the designator of the end marker of the linker set
158    identified by ``set``.  The item at the end marker address is not a member
159    of the linker set.  The ``set`` parameter itself must be a valid C
160    designator on which no macro expansion is performed.  It uniquely
161    identifies the linker set.
162
163.. raw:: latex
164
165   \clearpage
166
167.. _RTEMS_LINKER_SET_SIZE:
168
169RTEMS_LINKER_SET_SIZE - The linker set size in characters
170---------------------------------------------------------
171.. index:: RTEMS_LINKER_SET_SIZE
172
173CALLING SEQUENCE:
174    .. code-block:: c
175
176        size_t size = RTEMS_LINKER_SET_SIZE( set );
177
178DESCRIPTION:
179    This macro returns the size of the linker set identified by ``set`` in
180    characters.  The ``set`` parameter itself must be a valid C designator on
181    which no macro expansion is performed.  It uniquely identifies the linker
182    set.
183
184.. raw:: latex
185
186   \clearpage
187
188.. _RTEMS_LINKER_ROSET_DECLARE:
189
190RTEMS_LINKER_ROSET_DECLARE - Declares a read-only linker set
191------------------------------------------------------------
192.. index:: RTEMS_LINKER_ROSET_DECLARE
193
194CALLING SEQUENCE:
195    .. code-block:: c
196
197        RTEMS_LINKER_ROSET_DECLARE( set, type );
198
199DESCRIPTION:
200    This macro generates declarations for the begin and end markers of a
201    read-only linker set identified by ``set``.  The ``set`` parameter itself
202    must be a valid C designator on which no macro expansion is performed.  It
203    uniquely identifies the linker set. The ``type`` parameter defines the type
204    of the linker set items.  The type must be the same for all macro
205    invocations of a particular linker set.
206
207.. raw:: latex
208
209   \clearpage
210
211.. _RTEMS_LINKER_ROSET:
212
213RTEMS_LINKER_ROSET - Defines a read-only linker set
214---------------------------------------------------
215.. index:: RTEMS_LINKER_ROSET
216
217CALLING SEQUENCE:
218    .. code-block:: c
219
220        RTEMS_LINKER_ROSET( set, type );
221
222DESCRIPTION:
223    This macro generates definitions for the begin and end markers of a
224    read-only linker set identified by ``set``.  The ``set`` parameter itself
225    must be a valid C designator on which no macro expansion is performed.  It
226    uniquely identifies the linker set. The ``type`` parameter defines the type
227    of the linker set items.  The type must be the same for all macro
228    invocations of a particular linker set.
229
230.. raw:: latex
231
232   \clearpage
233
234.. _RTEMS_LINKER_ROSET_ITEM_DECLARE:
235
236RTEMS_LINKER_ROSET_ITEM_DECLARE - Declares a read-only linker set item
237----------------------------------------------------------------------
238.. index:: RTEMS_LINKER_ROSET_ITEM_DECLARE
239
240CALLING SEQUENCE:
241    .. code-block:: c
242
243        RTEMS_LINKER_ROSET_ITEM_DECLARE( set, type, item );
244
245DESCRIPTION:
246    This macro generates a declaration of an item contained in the read-only
247    linker set identified by ``set``.  The ``set`` parameter itself must be a
248    valid C designator on which no macro expansion is performed.  It uniquely
249    identifies the linker set. The ``type`` parameter defines the type of the
250    linker set items.  The type must be the same for all macro invocations of a
251    particular linker set. The ``item`` parameter itself must be a valid C
252    designator on which no macro expansion is performed.  It uniquely
253    identifies an item in the linker set.
254
255.. raw:: latex
256
257   \clearpage
258
259.. _RTEMS_LINKER_ROSET_ITEM_REFERENCE:
260
261RTEMS_LINKER_ROSET_ITEM_REFERENCE - References a read-only linker set item
262--------------------------------------------------------------------------
263.. index:: RTEMS_LINKER_ROSET_ITEM_REFERENCE
264
265CALLING SEQUENCE:
266    .. code-block:: c
267
268        RTEMS_LINKER_ROSET_ITEM_REFERENCE( set, type, item );
269
270DESCRIPTION:
271    This macro generates a reference to an item contained in the read-only
272    linker set identified by ``set``.  The ``set`` parameter itself must be a
273    valid C designator on which no macro expansion is performed.  It uniquely
274    identifies the linker set. The ``type`` parameter defines the type of the
275    linker set items.  The type must be the same for all macro invocations of a
276    particular linker set. The ``item`` parameter itself must be a valid C
277    designator on which no macro expansion is performed.  It uniquely
278    identifies an item in the linker set.
279
280.. raw:: latex
281
282   \clearpage
283
284.. _RTEMS_LINKER_ROSET_ITEM:
285
286RTEMS_LINKER_ROSET_ITEM - Defines a read-only linker set item
287-------------------------------------------------------------
288.. index:: RTEMS_LINKER_ROSET_ITEM
289
290CALLING SEQUENCE:
291    .. code-block:: c
292
293        RTEMS_LINKER_ROSET_ITEM( set, type, item );
294
295DESCRIPTION:
296    This macro generates a definition of an item contained in the read-only
297    linker set identified by ``set``.  The ``set`` parameter itself must be a
298    valid C designator on which no macro expansion is performed.  It uniquely
299    identifies the linker set. The ``type`` parameter defines the type of the
300    linker set items.  The type must be the same for all macro invocations of a
301    particular linker set. The ``item`` parameter itself must be a valid C
302    designator on which no macro expansion is performed.  It uniquely
303    identifies an item in the linker set.
304
305.. raw:: latex
306
307   \clearpage
308
309.. _RTEMS_LINKER_ROSET_ITEM_ORDERED:
310
311RTEMS_LINKER_ROSET_ITEM_ORDERED - Defines an ordered read-only linker set item
312------------------------------------------------------------------------------
313.. index:: RTEMS_LINKER_ROSET_ITEM_ORDERED
314
315CALLING SEQUENCE:
316    .. code-block:: c
317
318        RTEMS_LINKER_ROSET_ITEM_ORDERED( set, type, item, order );
319
320DESCRIPTION:
321    This macro generates a definition of an ordered item contained in the
322    read-only linker set identified by ``set``.  The ``set`` parameter itself
323    must be a valid C designator on which no macro expansion is performed.  It
324    uniquely identifies the linker set. The ``type`` parameter defines the type
325    of the linker set items.  The type must be the same for all macro
326    invocations of a particular linker set.  The ``item`` parameter itself must
327    be a valid C designator on which no macro expansion is performed.  It
328    uniquely identifies an item in the linker set. The ``order`` parameter must
329    be a valid linker input section name part on which macro expansion is
330    performed.  The items are lexicographically ordered according to the
331    ``order`` parameter within a linker set.  Ordered items are placed before
332    unordered items in the linker set.
333
334NOTES:
335    To be resilient to typos in the order parameter, it is recommended to use
336    the following construct in macros defining items for a particular linker
337    set (see enum in ``XYZ_ITEM()``).
338
339    .. code-block:: c
340
341        #include <rtems/linkersets.h>
342
343        typedef struct {
344            int foo;
345        } xyz_item;
346
347        /* The XYZ-order defines */
348        #define XYZ_ORDER_FIRST 0x00001000
349        #define XYZ_ORDER_AND_SO_ON 0x00002000
350
351        /* Defines an ordered XYZ-item */
352        #define XYZ_ITEM( item, order ) \
353                    enum { xyz_##item = order - order }; \
354                    RTEMS_LINKER_ROSET_ITEM_ORDERED( \
355                        xyz, const xyz_item *, item, order \
356                    ) = { &item }
357
358        /* Example item */
359        static const xyz_item some_item = { 123 };
360        XYZ_ITEM( some_item, XYZ_ORDER_FIRST );
361
362.. raw:: latex
363
364   \clearpage
365
366.. _RTEMS_LINKER_RWSET_DECLARE:
367
368RTEMS_LINKER_RWSET_DECLARE - Declares a read-write linker set
369-------------------------------------------------------------
370.. index:: RTEMS_LINKER_RWSET_DECLARE
371
372CALLING SEQUENCE:
373    .. code-block:: c
374
375        RTEMS_LINKER_RWSET_DECLARE( set, type );
376
377DESCRIPTION:
378    This macro generates declarations for the begin and end markers of a
379    read-write linker set identified by ``set``.  The ``set`` parameter itself
380    must be a valid C designator on which no macro expansion is performed.  It
381    uniquely identifies the linker set. The ``type`` parameter defines the type
382    of the linker set items.  The type must be the same for all macro
383    invocations of a particular linker set.
384
385.. raw:: latex
386
387   \clearpage
388
389.. _RTEMS_LINKER_RWSET:
390
391RTEMS_LINKER_RWSET - Defines a read-write linker set
392----------------------------------------------------
393.. index:: RTEMS_LINKER_RWSET
394
395CALLING SEQUENCE:
396    .. code-block:: c
397
398        RTEMS_LINKER_RWSET( set, type );
399
400DESCRIPTION:
401    This macro generates definitions for the begin and end markers of a
402    read-write linker set identified by ``set``.  The ``set`` parameter itself
403    must be a valid C designator on which no macro expansion is performed.  It
404    uniquely identifies the linker set. The ``type`` parameter defines the type
405    of the linker set items.  The type must be the same for all macro
406    invocations of a particular linker set.
407
408.. raw:: latex
409
410   \clearpage
411
412.. _RTEMS_LINKER_RWSET_ITEM_DECLARE:
413
414RTEMS_LINKER_RWSET_ITEM_DECLARE - Declares a read-write linker set item
415-----------------------------------------------------------------------
416.. index:: RTEMS_LINKER_RWSET_ITEM_DECLARE
417
418CALLING SEQUENCE:
419    .. code-block:: c
420
421        RTEMS_LINKER_RWSET_ITEM_DECLARE( set, type, item );
422
423DESCRIPTION:
424    This macro generates a declaration of an item contained in the read-write
425    linker set identified by ``set``.  The ``set`` parameter itself must be a
426    valid C designator on which no macro expansion is performed.  It uniquely
427    identifies the linker set. The ``type`` parameter defines the type of the
428    linker set items.  The type must be the same for all macro invocations of a
429    particular linker set. The ``item`` parameter itself must be a valid C
430    designator on which no macro expansion is performed.  It uniquely
431    identifies an item in the linker set.
432
433.. raw:: latex
434
435   \clearpage
436
437.. _RTEMS_LINKER_RWSET_ITEM_REFERENCE:
438
439RTEMS_LINKER_RWSET_ITEM_REFERENCE - References a read-write linker set item
440---------------------------------------------------------------------------
441.. index:: RTEMS_LINKER_RWSET_ITEM_REFERENCE
442
443CALLING SEQUENCE:
444    .. code-block:: c
445
446        RTEMS_LINKER_RWSET_ITEM_REFERENCE( set, type, item );
447
448DESCRIPTION:
449    This macro generates a reference to an item contained in the read-write
450    linker set identified by ``set``.  The ``set`` parameter itself must be a
451    valid C designator on which no macro expansion is performed.  It uniquely
452    identifies the linker set. The ``type`` parameter defines the type of the
453    linker set items.  The type must be the same for all macro invocations of a
454    particular linker set. The ``item`` parameter itself must be a valid C
455    designator on which no macro expansion is performed.  It uniquely
456    identifies an item in the linker set.
457
458.. raw:: latex
459
460   \clearpage
461
462.. _RTEMS_LINKER_RWSET_ITEM:
463
464RTEMS_LINKER_RWSET_ITEM - Defines a read-write linker set item
465--------------------------------------------------------------
466.. index:: RTEMS_LINKER_RWSET_ITEM
467
468CALLING SEQUENCE:
469    .. code-block:: c
470
471        RTEMS_LINKER_RWSET_ITEM( set, type, item );
472
473DESCRIPTION:
474    This macro generates a definition of an item contained in the read-write
475    linker set identified by ``set``.  The ``set`` parameter itself must be a
476    valid C designator on which no macro expansion is performed.  It uniquely
477    identifies the linker set. The ``type`` parameter defines the type of the
478    linker set items.  The type must be the same for all macro invocations of a
479    particular linker set. The ``item`` parameter itself must be a valid C
480    designator on which no macro expansion is performed.  It uniquely
481    identifies an item in the linker set.
482
483.. raw:: latex
484
485   \clearpage
486
487.. _RTEMS_LINKER_RWSET_ITEM_ORDERED:
488
489RTEMS_LINKER_RWSET_ITEM_ORDERED - Defines an ordered read-write linker set item
490-------------------------------------------------------------------------------
491.. index:: RTEMS_LINKER_RWSET_ITEM_ORDERED
492
493CALLING SEQUENCE:
494    .. code-block:: c
495
496        RTEMS_LINKER_RWSET_ITEM_ORDERED( set, type, item, order );
497
498DESCRIPTION:
499    This macro generates a definition of an ordered item contained in the
500    read-write linker set identified by ``set``.  The ``set`` parameter itself
501    must be a valid C designator on which no macro expansion is performed.  It
502    uniquely identifies the linker set. The ``type`` parameter defines the type
503    of the linker set items.  The type must be the same for all macro
504    invocations of a particular linker set.  The ``item`` parameter itself must
505    be a valid C designator on which no macro expansion is performed.  It
506    uniquely identifies an item in the linker set. The ``order`` parameter must
507    be a valid linker input section name part on which macro expansion is
508    performed.  The items are lexicographically ordered according to the
509    ``order`` parameter within a linker set.  Ordered items are placed before
510    unordered items in the linker set.
511
512NOTES:
513    To be resilient to typos in the order parameter, it is recommended to use
514    the following construct in macros defining items for a particular linker
515    set (see enum in ``XYZ_ITEM()``).
516
517    .. code-block:: c
518
519        #include <rtems/linkersets.h>
520
521        typedef struct {
522            int foo;
523        } xyz_item;
524
525        /* The XYZ-order defines */
526        #define XYZ_ORDER_FIRST 0x00001000
527        #define XYZ_ORDER_AND_SO_ON 0x00002000
528
529        /* Defines an ordered XYZ-item */
530        #define XYZ_ITEM( item, order ) \
531                    enum { xyz_##item = order - order }; \
532                    RTEMS_LINKER_RWSET_ITEM_ORDERED( \
533                        xyz, const xyz_item *, item, order \
534                    ) = { &item }
535        /* Example item */
536        static const xyz_item some_item = { 123 };
537        XYZ_ITEM( some_item, XYZ_ORDER_FIRST );
Note: See TracBrowser for help on using the repository browser.