summaryrefslogtreecommitdiffstats
path: root/c-user/linker_sets.rst
blob: 498aeb66b8e412a8242b223fda12cd6323803794 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
.. comment SPDX-License-Identifier: CC-BY-SA-4.0

.. COMMENT: COPYRIGHT (c) 1989-2014.
.. COMMENT: On-Line Applications Research Corporation (OAR).
.. COMMENT: All rights reserved.

.. _Linker Sets:

Linker Sets
***********

.. index:: linkersets

Introduction
============

Linker sets are a flexible means to create arrays of items out of a set of
object files at link-time.  For example its possible to define an item *I* of
type *T* in object file *A* and an item *J* of type *T* in object file *B* to
be a member of a linker set *S*.  The linker will then collect these two items
*I* and *J* and place them in consecutive memory locations, so that they can be
accessed like a normal array defined in one object file.  The size of a linker
set is defined by its begin and end markers.  A linker set may be empty.  It
should only contain items of the same type.

The following macros are provided to create, populate and use linker sets.

- RTEMS_LINKER_SET_BEGIN_ - Designator of the linker set begin marker

- RTEMS_LINKER_SET_END_ - Designator of the linker set end marker

- RTEMS_LINKER_SET_SIZE_ - The linker set size in characters

- RTEMS_LINKER_ROSET_DECLARE_ - Declares a read-only linker set

- RTEMS_LINKER_ROSET_ - Defines a read-only linker set

- RTEMS_LINKER_ROSET_ITEM_DECLARE_ - Declares a read-only linker set item

- RTEMS_LINKER_ROSET_ITEM_REFERENCE_ - References a read-only linker set item

- RTEMS_LINKER_ROSET_ITEM_ - Defines a read-only linker set item

- RTEMS_LINKER_ROSET_ITEM_ORDERED_ - Defines an ordered read-only linker set item

- RTEMS_LINKER_RWSET_DECLARE_ - Declares a read-write linker set

- RTEMS_LINKER_RWSET_ - Defines a read-write linker set

- RTEMS_LINKER_RWSET_ITEM_DECLARE_ - Declares a read-write linker set item

- RTEMS_LINKER_RWSET_ITEM_REFERENCE_ - References a read-write linker set item

- RTEMS_LINKER_RWSET_ITEM_ - Defines a read-write linker set item

- RTEMS_LINKER_RWSET_ITEM_ORDERED_ - Defines an ordered read-write linker set item

Background
==========

Linker sets are used not only in RTEMS, but also for example in Linux, in
FreeBSD, for the GNU C constructor extension and for global C++ constructors.
They provide a space efficient and flexible means to initialize modules.  A
linker set consists of

- dedicated input sections for the linker (e.g. ``.ctors`` and ``.ctors.*`` in
  the case of global constructors),

- a begin marker (e.g. provided by ``crtbegin.o``, and

- an end marker (e.g. provided by ``ctrend.o``).

A module may place a certain data item into the dedicated input section.  The
linker will collect all such data items in this section and creates a begin and
end marker.  The initialization code can then use the begin and end markers to
find all the collected data items (e.g. pointers to initialization functions).

In the linker command file of the GNU linker we need the following output
section descriptions.

.. code-block:: c

    /* To be placed in a read-only memory region */
    .rtemsroset : {
        KEEP (*(SORT(.rtemsroset.*)))
    }
    /* To be placed in a read-write memory region */
    .rtemsrwset : {
        KEEP (*(SORT(.rtemsrwset.*)))
    }

The ``KEEP()`` ensures that a garbage collection by the linker will not discard
the content of this section.  This would normally be the case since the linker
set items are not referenced directly.  The ``SORT()`` directive sorts the
input sections lexicographically.  Please note the lexicographical order of the
``.begin``, ``.content`` and ``.end`` section name parts in the RTEMS linker
sets macros which ensures that the position of the begin and end markers are
right.

So, what is the benefit of using linker sets to initialize modules?  It can be
used to initialize and include only those RTEMS managers and other components
which are used by the application.  For example, in case an application uses
message queues, it must call ``rtems_message_queue_create()``.  In the module
implementing this function, we can place a linker set item and register the
message queue handler constructor.  Otherwise, in case the application does not
use message queues, there will be no reference to the
``rtems_message_queue_create()`` function and the constructor is not
registered, thus nothing of the message queue handler will be in the final
executable.

For an example see test program :file:`sptests/splinkersets01`.

Directives
==========

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_SET_BEGIN:

RTEMS_LINKER_SET_BEGIN - Designator of the linker set begin marker
------------------------------------------------------------------
.. index:: RTEMS_LINKER_SET_BEGIN

CALLING SEQUENCE:
    .. code-block:: c

        volatile type *begin = RTEMS_LINKER_SET_BEGIN( set );

DESCRIPTION:
    This macro generates the designator of the begin marker of the linker set
    identified by ``set``.  The item at the begin marker address is the first
    member of the linker set if it exists, e.g. the linker set is not empty.  A
    linker set is empty, if and only if the begin and end markers have the same
    address.

    The ``set`` parameter itself must be a valid C designator on which no macro
    expansion is performed.  It uniquely identifies the linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_SET_END:

RTEMS_LINKER_SET_END - Designator of the linker set end marker
--------------------------------------------------------------
.. index:: RTEMS_LINKER_SET_END

CALLING SEQUENCE:
    .. code-block:: c

        volatile type *end = RTEMS_LINKER_SET_END( set );

DESCRIPTION:
    This macro generates the designator of the end marker of the linker set
    identified by ``set``.  The item at the end marker address is not a member
    of the linker set.  The ``set`` parameter itself must be a valid C
    designator on which no macro expansion is performed.  It uniquely
    identifies the linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_SET_SIZE:

RTEMS_LINKER_SET_SIZE - The linker set size in characters
---------------------------------------------------------
.. index:: RTEMS_LINKER_SET_SIZE

CALLING SEQUENCE:
    .. code-block:: c

        size_t size = RTEMS_LINKER_SET_SIZE( set );

DESCRIPTION:
    This macro returns the size of the linker set identified by ``set`` in
    characters.  The ``set`` parameter itself must be a valid C designator on
    which no macro expansion is performed.  It uniquely identifies the linker
    set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_ROSET_DECLARE:

RTEMS_LINKER_ROSET_DECLARE - Declares a read-only linker set
------------------------------------------------------------
.. index:: RTEMS_LINKER_ROSET_DECLARE

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_ROSET_DECLARE( set, type );

DESCRIPTION:
    This macro generates declarations for the begin and end markers of a
    read-only linker set identified by ``set``.  The ``set`` parameter itself
    must be a valid C designator on which no macro expansion is performed.  It
    uniquely identifies the linker set. The ``type`` parameter defines the type
    of the linker set items.  The type must be the same for all macro
    invocations of a particular linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_ROSET:

RTEMS_LINKER_ROSET - Defines a read-only linker set
---------------------------------------------------
.. index:: RTEMS_LINKER_ROSET

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_ROSET( set, type );

DESCRIPTION:
    This macro generates definitions for the begin and end markers of a
    read-only linker set identified by ``set``.  The ``set`` parameter itself
    must be a valid C designator on which no macro expansion is performed.  It
    uniquely identifies the linker set. The ``type`` parameter defines the type
    of the linker set items.  The type must be the same for all macro
    invocations of a particular linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_ROSET_ITEM_DECLARE:

RTEMS_LINKER_ROSET_ITEM_DECLARE - Declares a read-only linker set item
----------------------------------------------------------------------
.. index:: RTEMS_LINKER_ROSET_ITEM_DECLARE

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_ROSET_ITEM_DECLARE( set, type, item );

DESCRIPTION:
    This macro generates a declaration of an item contained in the read-only
    linker set identified by ``set``.  The ``set`` parameter itself must be a
    valid C designator on which no macro expansion is performed.  It uniquely
    identifies the linker set. The ``type`` parameter defines the type of the
    linker set items.  The type must be the same for all macro invocations of a
    particular linker set. The ``item`` parameter itself must be a valid C
    designator on which no macro expansion is performed.  It uniquely
    identifies an item in the linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_ROSET_ITEM_REFERENCE:

RTEMS_LINKER_ROSET_ITEM_REFERENCE - References a read-only linker set item
--------------------------------------------------------------------------
.. index:: RTEMS_LINKER_ROSET_ITEM_REFERENCE

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_ROSET_ITEM_REFERENCE( set, type, item );

DESCRIPTION:
    This macro generates a reference to an item contained in the read-only
    linker set identified by ``set``.  The ``set`` parameter itself must be a
    valid C designator on which no macro expansion is performed.  It uniquely
    identifies the linker set. The ``type`` parameter defines the type of the
    linker set items.  The type must be the same for all macro invocations of a
    particular linker set. The ``item`` parameter itself must be a valid C
    designator on which no macro expansion is performed.  It uniquely
    identifies an item in the linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_ROSET_ITEM:

RTEMS_LINKER_ROSET_ITEM - Defines a read-only linker set item
-------------------------------------------------------------
.. index:: RTEMS_LINKER_ROSET_ITEM

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_ROSET_ITEM( set, type, item );

DESCRIPTION:
    This macro generates a definition of an item contained in the read-only
    linker set identified by ``set``.  The ``set`` parameter itself must be a
    valid C designator on which no macro expansion is performed.  It uniquely
    identifies the linker set. The ``type`` parameter defines the type of the
    linker set items.  The type must be the same for all macro invocations of a
    particular linker set. The ``item`` parameter itself must be a valid C
    designator on which no macro expansion is performed.  It uniquely
    identifies an item in the linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_ROSET_ITEM_ORDERED:

RTEMS_LINKER_ROSET_ITEM_ORDERED - Defines an ordered read-only linker set item
------------------------------------------------------------------------------
.. index:: RTEMS_LINKER_ROSET_ITEM_ORDERED

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_ROSET_ITEM_ORDERED( set, type, item, order );

DESCRIPTION:
    This macro generates a definition of an ordered item contained in the
    read-only linker set identified by ``set``.  The ``set`` parameter itself
    must be a valid C designator on which no macro expansion is performed.  It
    uniquely identifies the linker set. The ``type`` parameter defines the type
    of the linker set items.  The type must be the same for all macro
    invocations of a particular linker set.  The ``item`` parameter itself must
    be a valid C designator on which no macro expansion is performed.  It
    uniquely identifies an item in the linker set. The ``order`` parameter must
    be a valid linker input section name part on which macro expansion is
    performed.  The items are lexicographically ordered according to the
    ``order`` parameter within a linker set.  Ordered items are placed before
    unordered items in the linker set.

NOTES:
    To be resilient to typos in the order parameter, it is recommended to use
    the following construct in macros defining items for a particular linker
    set (see enum in ``XYZ_ITEM()``).

    .. code-block:: c

        #include <rtems/linkersets.h>

        typedef struct {
            int foo;
        } xyz_item;

        /* The XYZ-order defines */
        #define XYZ_ORDER_FIRST 0x00001000
        #define XYZ_ORDER_AND_SO_ON 0x00002000

        /* Defines an ordered XYZ-item */
        #define XYZ_ITEM( item, order ) \
                    enum { xyz_##item = order - order }; \
                    RTEMS_LINKER_ROSET_ITEM_ORDERED( \
                        xyz, const xyz_item *, item, order \
                    ) = { &item }

        /* Example item */
        static const xyz_item some_item = { 123 };
        XYZ_ITEM( some_item, XYZ_ORDER_FIRST );

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_RWSET_DECLARE:

RTEMS_LINKER_RWSET_DECLARE - Declares a read-write linker set
-------------------------------------------------------------
.. index:: RTEMS_LINKER_RWSET_DECLARE

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_RWSET_DECLARE( set, type );

DESCRIPTION:
    This macro generates declarations for the begin and end markers of a
    read-write linker set identified by ``set``.  The ``set`` parameter itself
    must be a valid C designator on which no macro expansion is performed.  It
    uniquely identifies the linker set. The ``type`` parameter defines the type
    of the linker set items.  The type must be the same for all macro
    invocations of a particular linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_RWSET:

RTEMS_LINKER_RWSET - Defines a read-write linker set
----------------------------------------------------
.. index:: RTEMS_LINKER_RWSET

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_RWSET( set, type );

DESCRIPTION:
    This macro generates definitions for the begin and end markers of a
    read-write linker set identified by ``set``.  The ``set`` parameter itself
    must be a valid C designator on which no macro expansion is performed.  It
    uniquely identifies the linker set. The ``type`` parameter defines the type
    of the linker set items.  The type must be the same for all macro
    invocations of a particular linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_RWSET_ITEM_DECLARE:

RTEMS_LINKER_RWSET_ITEM_DECLARE - Declares a read-write linker set item
-----------------------------------------------------------------------
.. index:: RTEMS_LINKER_RWSET_ITEM_DECLARE

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_RWSET_ITEM_DECLARE( set, type, item );

DESCRIPTION:
    This macro generates a declaration of an item contained in the read-write
    linker set identified by ``set``.  The ``set`` parameter itself must be a
    valid C designator on which no macro expansion is performed.  It uniquely
    identifies the linker set. The ``type`` parameter defines the type of the
    linker set items.  The type must be the same for all macro invocations of a
    particular linker set. The ``item`` parameter itself must be a valid C
    designator on which no macro expansion is performed.  It uniquely
    identifies an item in the linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_RWSET_ITEM_REFERENCE:

RTEMS_LINKER_RWSET_ITEM_REFERENCE - References a read-write linker set item
---------------------------------------------------------------------------
.. index:: RTEMS_LINKER_RWSET_ITEM_REFERENCE

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_RWSET_ITEM_REFERENCE( set, type, item );

DESCRIPTION:
    This macro generates a reference to an item contained in the read-write
    linker set identified by ``set``.  The ``set`` parameter itself must be a
    valid C designator on which no macro expansion is performed.  It uniquely
    identifies the linker set. The ``type`` parameter defines the type of the
    linker set items.  The type must be the same for all macro invocations of a
    particular linker set. The ``item`` parameter itself must be a valid C
    designator on which no macro expansion is performed.  It uniquely
    identifies an item in the linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_RWSET_ITEM:

RTEMS_LINKER_RWSET_ITEM - Defines a read-write linker set item
--------------------------------------------------------------
.. index:: RTEMS_LINKER_RWSET_ITEM

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_RWSET_ITEM( set, type, item );

DESCRIPTION:
    This macro generates a definition of an item contained in the read-write
    linker set identified by ``set``.  The ``set`` parameter itself must be a
    valid C designator on which no macro expansion is performed.  It uniquely
    identifies the linker set. The ``type`` parameter defines the type of the
    linker set items.  The type must be the same for all macro invocations of a
    particular linker set. The ``item`` parameter itself must be a valid C
    designator on which no macro expansion is performed.  It uniquely
    identifies an item in the linker set.

.. raw:: latex

   \clearpage

.. _RTEMS_LINKER_RWSET_ITEM_ORDERED:

RTEMS_LINKER_RWSET_ITEM_ORDERED - Defines an ordered read-write linker set item
-------------------------------------------------------------------------------
.. index:: RTEMS_LINKER_RWSET_ITEM_ORDERED

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_LINKER_RWSET_ITEM_ORDERED( set, type, item, order );

DESCRIPTION:
    This macro generates a definition of an ordered item contained in the
    read-write linker set identified by ``set``.  The ``set`` parameter itself
    must be a valid C designator on which no macro expansion is performed.  It
    uniquely identifies the linker set. The ``type`` parameter defines the type
    of the linker set items.  The type must be the same for all macro
    invocations of a particular linker set.  The ``item`` parameter itself must
    be a valid C designator on which no macro expansion is performed.  It
    uniquely identifies an item in the linker set. The ``order`` parameter must
    be a valid linker input section name part on which macro expansion is
    performed.  The items are lexicographically ordered according to the
    ``order`` parameter within a linker set.  Ordered items are placed before
    unordered items in the linker set.

NOTES:
    To be resilient to typos in the order parameter, it is recommended to use
    the following construct in macros defining items for a particular linker
    set (see enum in ``XYZ_ITEM()``).

    .. code-block:: c

        #include <rtems/linkersets.h>

        typedef struct {
            int foo;
        } xyz_item;

        /* The XYZ-order defines */
        #define XYZ_ORDER_FIRST 0x00001000
        #define XYZ_ORDER_AND_SO_ON 0x00002000

        /* Defines an ordered XYZ-item */
        #define XYZ_ITEM( item, order ) \
                    enum { xyz_##item = order - order }; \
                    RTEMS_LINKER_RWSET_ITEM_ORDERED( \
                        xyz, const xyz_item *, item, order \
                    ) = { &item }
        /* Example item */
        static const xyz_item some_item = { 123 };
        XYZ_ITEM( some_item, XYZ_ORDER_FIRST );