summaryrefslogtreecommitdiff
path: root/rld-outputter.cpp
blob: 600aedcddfabef4ab45221600445ecd6329db3c6 (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
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
/*
 * Copyright (c) 2011, Chris Johns <chrisj@rtems.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/**
 * @file
 *
 * @ingroup rtems_ld
 *
 * @brief RTEMS Linker.
 *
 */

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

#include <fstream>
#include <iostream>

#include <errno.h>
#include <string.h>

#include <rld.h>
#include <rld-rap.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "rld-process.h"

namespace rld
{
  namespace outputter
  {
    int unlink (const char* path)
    {
#if _WIN32
      return ::remove(path);
#else
      return ::unlink (path);
#endif
    }

    int link (const char* path1, const char* path2)
    {
#if _WIN32
      return ::rename(path1, path2);
#else
      return ::link (path1, path2);
#endif
    }

    const std::string
    script_text (const std::string&        entry,
                 const std::string&        exit,
                 const files::object_list& dependents,
                 const files::cache&       cache,
                 bool                      not_in_archive)
    {
      std::ostringstream out;
      files::object_list objects;
      files::object_list dep_copy (dependents);

      cache.get_objects (objects);
      objects.merge (dep_copy);
      objects.unique ();

      if (rld::verbose () >= RLD_VERBOSE_INFO)
        std::cout << " E: " << entry << std::endl;

      out << "E: " << entry << std::endl;

      if (!exit.empty ())
      {
        if (rld::verbose () >= RLD_VERBOSE_INFO)
          std::cout << " e: " << exit << std::endl;
        out << "e: " << exit << std::endl;
      }

      for (files::object_list::iterator oi = objects.begin ();
           oi != objects.end ();
           ++oi)
      {
        files::object& obj = *(*oi);
        std::string    name = obj.name ().basename ();

        if (not_in_archive)
        {
          size_t pos = name.find (':');
          if (pos != std::string::npos)
            name[pos] = '_';
          pos = name.find ('@');
          if (pos != std::string::npos)
            name = name.substr (0, pos);
        }

        if (rld::verbose () >= RLD_VERBOSE_INFO)
          std::cout << " o: " << name << std::endl;

        out << "o:" << name << std::endl;

        symbols::symtab& unresolved = obj.unresolved_symbols ();

        int count = 0;
        for (symbols::symtab::iterator ursi = unresolved.begin ();
             ursi != unresolved.begin ();
             ++ursi)
        {
          symbols::symbol& urs = *((*ursi).second);

          ++count;

          if (rld::verbose () >= RLD_VERBOSE_INFO)
            std::cout << " u: " << count << ':' << urs.name () << std::endl;

          out << " u:" << count << ':' << urs.name () << std::endl;
        }
      }

      return out.str ();
    }

    void
    metadata_object (files::object&            metadata,
                     const std::string&        entry,
                     const std::string&        exit,
                     const files::object_list& dependents,
                     const files::cache&       cache)
    {
      if (rld::verbose () >= RLD_VERBOSE_INFO)
        std::cout << "metadata: " << metadata.name ().full () << std::endl;

      const std::string script =
        script_text (entry, exit, dependents, cache, true);

      metadata.open (true);
      metadata.begin ();

      elf::file& elf = metadata.elf ();

      elf.set_header (ET_EXEC,
                      elf::object_class (),
                      elf::object_datatype (),
                      elf::object_machine_type ());

      elf::section md (elf,
                       elf.section_count () + 1,
                       ".rtemsmd",
                       SHT_STRTAB,
                       1,
                       0,
                       0,
                       0,
                       script.length ());

      md.add_data (ELF_T_BYTE,
                   1,
                   script.length (),
                   (void*) script.c_str ());

      elf.add (md);
      elf.write ();

      metadata.end ();
      metadata.close ();
    }

    void
    archive (const std::string&        name,
             const std::string&        entry,
             const std::string&        exit,
             const files::object_list& dependents,
             const files::cache&       cache)
    {
      if (rld::verbose () >= RLD_VERBOSE_INFO)
        std::cout << "outputter:archive: " << name
                  << ", dependents: " << dependents.size () << std::endl;

      std::string ext = path::extension (name);
      std::string mdname =
        name.substr (0, name.length () - ext.length ()) + "-metadata.o";

      files::object metadata (mdname);

      metadata_object (metadata, entry, exit, dependents, cache);

      files::object_list dep_copy (dependents);
      files::object_list objects;

      cache.get_objects (objects);
      objects.merge (dep_copy);
      objects.push_front (&metadata);
      objects.unique ();

      files::archive arch (name);
      arch.create (objects);
    }

    void
    archivera (const std::string&        name,
               const files::object_list& dependents,
               files::cache&             cache,
               bool                      ra_exist,
               bool                      ra_rap)
    {
      files::object_list dep_copy (dependents);
      files::object_list objects;

      if (rld::verbose () >= RLD_VERBOSE_INFO)
        std::cout << "outputter:archivera: " << name
                  << ", dependents: " << dependents.size () << std::endl;

      objects.clear ();

      files::object_list::iterator oli;
      for (oli = dep_copy.begin (); oli != dep_copy.end (); ++oli)
      {
        files::object& object = *(*oli);

        if (ra_rap)
          objects.push_back (&object);
        else
        {
          if (object.get_archive ())
            objects.push_back (&object);
        }
      }

      bool exist = false;
      files::object_list objects_tmp;
      files::objects& objs = cache.get_objects ();
      objects_tmp.clear ();
      for (files::objects::iterator obi = objs.begin ();
           obi != objs.end ();
           ++obi)
      {
        files::object* obj = (*obi).second;
        exist = false;

        /**
         * Replace the elf object file in ra file with elf object file
         * in collected object files, if exist.
         */
        if (!ra_rap)
        {
          for (oli = objects.begin (); oli != objects.end (); ++oli)
          {
            files::object& object = *(*oli);
            if (obj->name ().oname () == object.name ().oname ())
            {
              exist = true;
              break;
            }
          }
        }

        if (!exist)
          objects_tmp.push_back (obj);
      }

      objects.merge (objects_tmp);
      objects.unique ();

      if (objects.size ())
      {
        if (ra_exist)
        {
          std::string    new_name = "rld_XXXXXX";
          files::archive arch (new_name);
          struct stat    sb;

          arch.create (objects);

          if ((::stat (name.c_str (), &sb) >= 0) && S_ISREG (sb.st_mode))
          {
            if (unlink (name.c_str ()) < 0)
              std::cerr << "error: unlinking temp file: " << name << std::endl;
          }
          if (link (new_name.c_str (), name.c_str ()) < 0)
          {
            std::cerr << "error: linking temp file: " << name << std::endl;
          }
          if ((::stat (new_name.c_str (), &sb) >= 0) && S_ISREG (sb.st_mode))
          {
            if (unlink (new_name.c_str ()) < 0)
              std::cerr << "error: unlinking temp file: " << new_name << std::endl;
          }
        }
        else
        {
          /* Create */
          files::archive arch (name);
          arch.create (objects);
        }
      }
    }

    void
    script (const std::string&        name,
            const std::string&        entry,
            const std::string&        exit,
            const files::object_list& dependents,
            const files::cache&       cache)
    {
      if (rld::verbose () >= RLD_VERBOSE_INFO)
        std::cout << "outputter:script: " << name << std::endl;

      std::fstream out (name.c_str (),
                        std::ios_base::out | std::ios_base::trunc);

      /*
       * Tag for the shell to use.
       */
      out << "!# rls" << std::endl;

      try
      {
        out << script_text (entry, exit, dependents, cache, false);
      }
      catch (...)
      {
        out.close ();
        throw;
      }

      out.close ();
    }

    void
    elf_application (const std::string&        name,
                     const std::string&        entry,
                     const std::string&        exit,
                     const files::object_list& dependents,
                     const files::cache&       cache)
    {
      if (rld::verbose () >= RLD_VERBOSE_INFO)
        std::cout << "outputter:application: " << name << std::endl;

      files::object_list dep_copy (dependents);
      files::object_list objects;
      std::string        header;
      std::string        script;
      files::image       app (name);

      header = "RELF,00000000,0001,none,00000000\n";
      header += '\0';

      script = script_text (entry, exit, dependents, cache, true);

      cache.get_objects (objects);
      objects.merge (dep_copy);
      objects.unique ();

      app.open (true);
      app.write (header.c_str (), header.size ());

      #define APP_BUFFER_SIZE  (128 * 1024)

      uint8_t* buffer = 0;

      try
      {
        buffer = new uint8_t[APP_BUFFER_SIZE];

        for (files::object_list::iterator oi = objects.begin ();
             oi != objects.end ();
             ++oi)
        {
          files::object& obj = *(*oi);

          obj.open ();

          try
          {
            obj.seek (0);

            size_t in_size = obj.name ().size ();

            while (in_size)
            {
              size_t reading =
                in_size < APP_BUFFER_SIZE ? in_size : APP_BUFFER_SIZE;

              app.write (buffer, obj.read (buffer, reading));

              in_size -= reading;
            }
          }
          catch (...)
          {
            obj.close ();
            throw;
          }

          obj.close ();
        }
      }
      catch (...)
      {
        delete [] buffer;
        app.close ();
        throw;
      }

      delete [] buffer;

      app.close ();
    }

    bool in_archive (files::object* object)
    {
      if (object->get_archive ())
        return true;
      return false;
    }

    void
    application (const std::string&        name,
                 const std::string&        entry,
                 const std::string&        exit,
                 const files::object_list& dependents,
                 const files::cache&       cache,
                 const symbols::table&     symbols,
                 bool                      one_file)
    {
      if (rld::verbose () >= RLD_VERBOSE_INFO)
        std::cout << "outputter:application: " << name << std::endl;

      files::object_list dep_copy (dependents);
      files::object_list objects;
      files::image       app (name);

      if (!one_file)
        dep_copy.remove_if (in_archive);

      cache.get_objects (objects);
      objects.merge (dep_copy);
      objects.sort ();
      objects.unique ();

      app.open (true);

      try
      {
        rap::write (app, entry, exit, objects, symbols);
      }
      catch (...)
      {
        app.close ();
        throw;
      }

      app.close ();
    }

  }
}