source: rtems-docs/c_user/linker_sets.rst @ 0d86b71

4.115
Last change on this file since 0d86b71 was 0d86b71, checked in by Chris Johns <chrisj@…>, on 02/18/16 at 04:29:28

Clean up.

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