aboutsummaryrefslogtreecommitdiff
path: root/gold/testsuite/plugin_section_alignment.cc
blob: 76b64ecf0f820b7cc282a9c50c92e07da460bd8e (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
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
// plugin_section_alignment.cc -- plugins to test ordering with {size,alignment}
//
// Copyright (C) 2016-2017 Free Software Foundation, Inc.
// Written by Than McIntosh <thanm@google.com>.
//
// This file is part of gold.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.
//

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <string>
#include <vector>
#include <algorithm>

#include "plugin-api.h"

static ld_plugin_get_input_section_count get_input_section_count = NULL;
static ld_plugin_get_input_section_alignment get_input_section_alignment = NULL;
static ld_plugin_get_input_section_size get_input_section_size = NULL;
static ld_plugin_get_input_section_name get_input_section_name = NULL;
static ld_plugin_update_section_order update_section_order = NULL;
static ld_plugin_allow_section_ordering allow_section_ordering = NULL;

extern "C" {
  enum ld_plugin_status onload(struct ld_plugin_tv *tv);
  enum ld_plugin_status claim_file_hook(const struct ld_plugin_input_file *file,
                                        int *claimed);
  enum ld_plugin_status all_symbols_read_hook(void);
}

typedef enum { FL_BSS, FL_DATA, FL_RODATA, FL_UNKNOWN } sec_flavor;

inline static int is_prefix_of(const char *prefix, const char *str)
{
  return strncmp(prefix, str, strlen(prefix)) == 0;
}

inline static sec_flavor flavor_from_name(const char *secname)
{
  if (is_prefix_of(".data.v", secname)) {
    return FL_DATA;
  } else if (is_prefix_of(".bss.v", secname)) {
    return FL_BSS;
  } else if (is_prefix_of(".rodata.v", secname)) {
    return FL_RODATA;
  } else {
    return FL_UNKNOWN;
  }
}

struct SectionInfo {
  ld_plugin_section plugin_section;
  std::string name;
  uint64_t size;
  sec_flavor flavor;
  unsigned align;

  static bool SectionInfoLt(const SectionInfo &i1,
                            const SectionInfo &i2)
  {
    if (i1.flavor != i2.flavor) {
      return ((unsigned) i1.flavor) < ((unsigned) i2.flavor);
    }
    switch (i1.flavor) {
      case FL_DATA:
        // Sort data items by increasing alignment then increasing size
        if (i1.align != i2.align) {
          return ((unsigned) i1.align) < ((unsigned) i2.align);
        }
        if (i1.size != i2.size) {
          return ((unsigned) i1.size) < ((unsigned) i2.size);
        }
        break;
      case FL_BSS:
        // Sort bss items by decreasing alignment then decreasing size
        if (i1.align != i2.align) {
          return ((unsigned) i2.align) < ((unsigned) i1.align);
        }
        if (i1.size != i2.size) {
          return ((unsigned) i2.size) < ((unsigned) i1.size);
        }
        break;
      case FL_RODATA:
        // Sort rodata items by decreasing alignment then increasing size
        if (i1.align != i2.align) {
          return ((unsigned) i2.align) < ((unsigned) i1.align);
        }
        if (i1.size != i2.size) {
          return ((unsigned) i1.size) < ((unsigned) i2.size);
        }
      default:
        break;
    }

    // Break ties by name
    return i1.name.compare(i2.name) < 0;
  }

};
typedef std::vector<SectionInfo> section_info_vector;
section_info_vector raw_sections;

// Set to 1 for debugging output
unsigned trace = 0;

extern "C" {

// Plugin entry point.
enum ld_plugin_status onload(struct ld_plugin_tv *tv)
{
  struct ld_plugin_tv *entry;
  for (entry = tv; entry->tv_tag != LDPT_NULL; ++entry)
  {
    switch (entry->tv_tag)
    {
      case LDPT_OPTION:
        if (!strcmp(entry->tv_u.tv_string,"-trace")) {
          fprintf(stderr, "enabling tracing\n");
          trace += 1;
        } else {
          fprintf(stderr, "unknown plugin option %s", entry->tv_u.tv_string);
        }
        break;
      case LDPT_REGISTER_CLAIM_FILE_HOOK:
        assert((*entry->tv_u.tv_register_claim_file)(claim_file_hook) ==
               LDPS_OK);
        break;
      case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
        assert((*entry->tv_u.tv_register_all_symbols_read)(
            all_symbols_read_hook) == LDPS_OK);
        break;
      case LDPT_GET_INPUT_SECTION_COUNT:
        get_input_section_count = *entry->tv_u.tv_get_input_section_count;
        break;
      case LDPT_GET_INPUT_SECTION_NAME:
        get_input_section_name = *entry->tv_u.tv_get_input_section_name;
        break;
      case LDPT_GET_INPUT_SECTION_ALIGNMENT:
        get_input_section_alignment =
            *entry->tv_u.tv_get_input_section_alignment;
        break;
      case LDPT_GET_INPUT_SECTION_SIZE:
        get_input_section_size = *entry->tv_u.tv_get_input_section_size;
        break;
      case LDPT_UPDATE_SECTION_ORDER:
        update_section_order = *entry->tv_u.tv_update_section_order;
        break;
      case LDPT_ALLOW_SECTION_ORDERING:
        allow_section_ordering = *entry->tv_u.tv_allow_section_ordering;
        break;
      default:
        break;
    }
  }

  if (get_input_section_count == NULL || get_input_section_alignment == NULL ||
      get_input_section_size == NULL || get_input_section_name == NULL ||
      update_section_order == NULL || allow_section_ordering == NULL) {
    fprintf(stderr, "Some interfaces are missing\n");
    return LDPS_ERR;
  }

  return LDPS_OK;
}

// This function is called by the linker for every new object it encounters.
enum ld_plugin_status claim_file_hook(const struct ld_plugin_input_file *file,
                                      int *claimed)
{
  static bool is_ordering_specified = false;
  struct ld_plugin_section section;
  unsigned count = 0;
  unsigned shndx;

  *claimed = 0;
  if (!is_ordering_specified) {
    // Inform the linker to prepare for section reordering.
    (*allow_section_ordering)();
    is_ordering_specified = true;
  }

  (*get_input_section_count)(file->handle, &count);

  for (shndx = 0; shndx < count; ++shndx) {
    char *name = NULL;

    section.handle = file->handle;
    section.shndx = shndx;
    (*get_input_section_name)(section, &name);
    if (is_prefix_of(".data.v", name) ||
        is_prefix_of(".bss.v", name) ||
        is_prefix_of(".rodata.v", name)) {
      SectionInfo si;
      si.plugin_section.handle = file->handle;
      si.plugin_section.shndx = shndx;
      (*get_input_section_size)(section, &si.size);
      (*get_input_section_alignment)(section, &si.align);
      si.name = name;
      si.flavor = flavor_from_name(name);
      raw_sections.push_back(si);
    }
  }

  return LDPS_OK;
}

// This function is called by the linker after all the symbols have been read.
// At this stage, it is fine to tell the linker the desired function order.

enum ld_plugin_status all_symbols_read_hook(void)
{
  // We expect to see a total of twelve sections -- if this is not the case
  // then something went wrong somewhere along the way.
  if (raw_sections.size() != 12) {
    fprintf(stderr, "Expected 12 sections, found %u sections",
            (unsigned) raw_sections.size());
    return LDPS_ERR;
  }

  std::sort(raw_sections.begin(), raw_sections.end(),
            SectionInfo::SectionInfoLt);

  struct ld_plugin_section section_list[12];
  for (unsigned ii = 0; ii < 12; ++ii) {
    section_list[ii] = raw_sections[ii].plugin_section;
  }

  if (trace) {
    fprintf(stderr, "Specified section order is:\n");
    for (section_info_vector::iterator it = raw_sections.begin();
         it != raw_sections.end();
         ++it) {
      const SectionInfo &si = (*it);
      fprintf(stderr, "Idx=%u Name=%s Align=%u Size=%u\n",
              si.plugin_section.shndx, si.name.c_str(), si.align,
              (unsigned) si.size);
    }
  }

  update_section_order(section_list, 12);

  return LDPS_OK;
}

} // end extern "C"