summaryrefslogtreecommitdiff
path: root/mksyms.awk
blob: 99bce48ae950df0f234628e9c3844ce7361f4ed9 (plain)
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
#
#  COPYRIGHT (c) 2012 Chris Johns <chrisj@rtems.org>
#
#  The license and distribution terms for this file may be
#  found in the file LICENSE in this distribution or at
#  http://www.rtems.com/license/LICENSE.
#
# Covert the list of symbols into a C structure.
#
#

function c_header()
{
  print ("/*");
  print (" * RTEMS Global Symbol Table");
  print (" *  Automatically generated. Do not edit, just regenerate.");
  print (" */");
  print ("");
  print ("extern unsigned char __rtems_rtl_base_globals[];");
  print ("extern unsigned int __rtems_rtl_base_globals_size;");
  print ("");
  print ("asm(\"  .align   4\");");
  print ("asm(\"__rtems_rtl_base_globals:\");");
}

function c_trailer()
{
  print ("asm(\"  .byte    0\");");
  print ("asm(\"  .align 0\");");
  print ("asm(\"  .ascii \\\"\\xde\\xad\\xbe\\xef\\\"\");");
  print ("asm(\"  .align   4\");");
  print ("asm(\"__rtems_rtl_base_globals_size:\");");
  print ("asm(\"  .long __rtems_rtl_base_globals_size - __rtems_rtl_base_globals\");");
  print ("");
  print ("void rtems_rtl_base_sym_global_add (const unsigned char* , unsigned int );");
}

function c_rtl_call_body()
{
  print ("{");
  print ("  rtems_rtl_base_sym_global_add (__rtems_rtl_base_globals,");
  print ("                                 __rtems_rtl_base_globals_size);");
  print ("}");
}

function c_constructor_trailer()
{
  c_trailer();
  print ("static void init(void) __attribute__ ((constructor));");
  print ("static void init(void)");
  c_rtl_call_body();
}

function c_embedded_trailer()
{
  c_trailer();
  print ("void rtems_rtl_base_global_syms_init(void);");
  print ("void rtems_rtl_base_global_syms_init(void)");
  c_rtl_call_body();
}

BEGIN {
  FS = "[ \t\n]";
  OFS = " ";
  started = 0
  embed = 0
  for (a = 0; a < ARGC; ++a)
  {
    if (ARGV[a] == "--embed")
    {
      embed = 1
      delete ARGV[a];
    }
    else if (ARGV[a] != "-" && ARGV[a] != "awk")
    {
      print ("invalid option:", ARGV[a]);
      exit 2
    }
  }
  c_header();
  syms = 0
  started = 1
}
END {
  if (started)
  {
    for (s = 0; s < syms; ++s)
    {
      printf ("asm(\"  .asciz \\\"%s\\\"\");\n", symbols[s]);
      if (embed)
      {
        printf ("asm(\"  .align 0\");\n");
        printf ("asm(\"  .long %s\");\n", symbols[s]);
      }
      else
        printf ("asm(\"  .long 0x%s\");\n", addresses[s]);
    }
    if (embed)
      c_embedded_trailer();
    else
      c_constructor_trailer();
  }
}

#
# Parse the output of 'nm'
#

{
  #
  # We need 3 fields
  #
  if (NF == 3)
  {
    if (($3 != "__DTOR_END__") ||
        ($3 != "__CTOR_END__") ||
        ($3 != "__DYNAMIC"))
    {
      symbols[syms] = $3;
      addresses[syms] = $1;
      ++syms;
    }
  }
}