Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #43791

Re: can linker symbols be defined in C?

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: can linker symbols be defined in C?
Date 2014-04-29 00:20 +0000
Organization Aioe.org NNTP Server
Message-ID <20140428170342.745@kylheku.com> (permalink)
References <2231b9bc-305e-4ed5-af87-e203dd64780a@googlegroups.com>

Show all headers | View raw


On 2014-04-28, Ralph Doncaster <ralphdoncaster@gmail.com> wrote:
> I want to define linker symbols in C.  I can do it using gcc inline assembler:
> #define STR1(x) #x
> #define STR(x) STR1(x)
> ... more macros that calculate TXDELAYCOUNT
> asm(".global TXDELAY" );
> asm(".equ TXDELAY, " STR(TXDELAYCOUNT) );

Okay, so here we calculate a name somehow (perhaps based on some
platform configuration preprocessor symbols) and make TXDELAY
and alias for that symbol.

There are less direct ways to do that, which are very portable. E.g.

  /* header */
  extern delay_t *tx_delay_loc;
  #define tx_delay (*tx_delay_loc)

  /* platform-specific .c file */
  delay_t *tx_delay_loc = <platform specific value>;

Or, if it can't be statically determined, the module init routine
could do it:

  void board_init(void)
  {
    tx_delay_loc = <whatever>;
  }

If the location is simple constant memory address, it can be something like:

  #if CONFIG_SUCH_AND_SUCH
  #define tx_delay_loc_inline = ((unsigned long *) SUCH_AND_SUCH_TX_DELAY_REG)
  #elif CONFIG_OTHER
  ...
  #endif

  #define tx_delay (*tx_delay_loc_inline)

These techniques are not incompatible with doing it using toolchain-specific
linker tricks also, on some of the targets where that is an option.

In C++ you can abuse a reference to create an alias:

  static delay_t &tx_delay = *tx_delay_loc_inline;

Or, perhaps, not abuse them at all:

  #if CONFIG_SUCH_AND_SUCH
  /* assuming Such and Such platform has a declared SUCH_AND_SUC_TX_DELAY
     as a global variable of type long */
  static long &tx_delay = SUCH_AND_SUCH_TX_DELAY;
  #endif

I.e. C++ references at file scope de facto provide a similar capability to
linker-level symbol aliasing (at least for objects, not functions).

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-28 13:53 -0700
  Re: can linker symbols be defined in C? James Kuyper <jameskuyper@verizon.net> - 2014-04-28 17:15 -0400
  Re: can linker symbols be defined in C? Andrew Smallshaw <andrews@sdf.lonestar.org> - 2014-04-28 21:17 +0000
    Re: can linker symbols be defined in C? Keith Thompson <kst-u@mib.org> - 2014-04-28 14:53 -0700
      Re: can linker symbols be defined in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-28 23:28 +0000
    Re: can linker symbols be defined in C? James Kuyper <jameskuyper@verizon.net> - 2014-04-28 18:01 -0400
  Re: can linker symbols be defined in C? jacob navia <jacob@spamsink.net> - 2014-04-29 00:26 +0200
    Re: can linker symbols be defined in C? "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-28 19:39 -0400
    Re: can linker symbols be defined in C? Ian Collins <ian-news@hotmail.com> - 2014-04-29 11:45 +1200
    Re: can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-28 20:15 -0700
      Re: can linker symbols be defined in C? Kaz Kylheku <kaz@kylheku.com> - 2014-04-29 04:11 +0000
        Re: can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-29 16:29 -0700
      Re: can linker symbols be defined in C? Noob <root@127.0.0.1> - 2014-04-29 08:33 +0200
  Re: can linker symbols be defined in C? Siri Crews <chine.bleu@yahoo.com> - 2014-04-28 16:54 -0700
    Re: can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-28 20:01 -0700
  Re: can linker symbols be defined in C? Kaz Kylheku <kaz@kylheku.com> - 2014-04-29 00:20 +0000
    Re: can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-28 19:57 -0700

csiph-web