]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/readsyms.cc
Snapshot. Now able to produce a minimal executable which actually
[thirdparty/binutils-gdb.git] / gold / readsyms.cc
1 // readsyms.cc -- read input file symbols for gold
2
3 #include "gold.h"
4
5 #include <cstring>
6
7 #include "elfcpp.h"
8 #include "options.h"
9 #include "dirsearch.h"
10 #include "object.h"
11 #include "archive.h"
12 #include "readsyms.h"
13
14 namespace gold
15 {
16
17 // Class read_symbols.
18
19 Read_symbols::~Read_symbols()
20 {
21 // The this_blocker_ and next_blocker_ pointers are passed on to the
22 // Add_symbols task.
23 }
24
25 // Return whether a Read_symbols task is runnable. We need write
26 // access to the symbol table. We can read an ordinary input file
27 // immediately. For an archive specified using -l, we have to wait
28 // until the search path is complete.
29
30 Task::Is_runnable_type
31 Read_symbols::is_runnable(Workqueue*)
32 {
33 if (this->input_.is_lib() && this->dirpath_.token().is_blocked())
34 return IS_BLOCKED;
35
36 return IS_RUNNABLE;
37 }
38
39 // Return a Task_locker for a Read_symbols task. We don't need any
40 // locks here.
41
42 Task_locker*
43 Read_symbols::locks(Workqueue*)
44 {
45 return NULL;
46 }
47
48 // Run a Read_symbols task. This is where we actually read the
49 // symbols and relocations.
50
51 void
52 Read_symbols::run(Workqueue* workqueue)
53 {
54 Input_file* input_file = new Input_file(this->input_);
55 input_file->open(this->options_, this->dirpath_);
56
57 // Read enough of the file to pick up the entire ELF header.
58
59 int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
60 off_t bytes;
61 const unsigned char* p = input_file->file().get_view(0, ehdr_size, &bytes);
62 if (bytes >= 4)
63 {
64 static unsigned char elfmagic[4] =
65 {
66 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
67 elfcpp::ELFMAG2, elfcpp::ELFMAG3
68 };
69 if (memcmp(p, elfmagic, 4) == 0)
70 {
71 // This is an ELF object.
72 Object* obj = make_elf_object(this->input_.name(), input_file, 0,
73 p, bytes);
74
75 this->input_objects_->add_object(obj);
76
77 Read_symbols_data sd = obj->read_symbols();
78 workqueue->queue(new Add_symbols(this->symtab_, obj, sd,
79 this->this_blocker_,
80 this->next_blocker_));
81
82 // Opening the file locked it, so now we need to unlock it.
83 input_file->file().unlock();
84
85 return;
86 }
87 }
88
89 if (bytes >= Archive::sarmag)
90 {
91 if (memcmp(p, Archive::armag, Archive::sarmag) == 0)
92 {
93 // This is an archive.
94 Archive* arch = new Archive(this->input_.name(), input_file);
95 arch->setup();
96 workqueue->queue(new Add_archive_symbols(this->symtab_,
97 this->input_objects_,
98 arch,
99 this->this_blocker_,
100 this->next_blocker_));
101 return;
102 }
103 }
104
105 // Here we have to handle archives and any other input file
106 // types we need.
107 fprintf(stderr, _("%s: %s: not an object or archive\n"),
108 program_name, input_file->file().filename().c_str());
109 gold_exit(false);
110 }
111
112 // Class Add_symbols.
113
114 Add_symbols::~Add_symbols()
115 {
116 if (this->this_blocker_ != NULL)
117 delete this->this_blocker_;
118 // next_blocker_ is deleted by the task associated with the next
119 // input file.
120 }
121
122 // We are blocked by this_blocker_. We block next_blocker_. We also
123 // lock the file.
124
125 Task::Is_runnable_type
126 Add_symbols::is_runnable(Workqueue*)
127 {
128 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
129 return IS_BLOCKED;
130 if (this->object_->is_locked())
131 return IS_LOCKED;
132 return IS_RUNNABLE;
133 }
134
135 class Add_symbols::Add_symbols_locker : public Task_locker
136 {
137 public:
138 Add_symbols_locker(Task_token& token, Workqueue* workqueue,
139 Object* object)
140 : blocker_(token, workqueue), objlock_(*object)
141 { }
142
143 private:
144 Task_locker_block blocker_;
145 Task_locker_obj<Object> objlock_;
146 };
147
148 Task_locker*
149 Add_symbols::locks(Workqueue* workqueue)
150 {
151 return new Add_symbols_locker(*this->next_blocker_, workqueue,
152 this->object_);
153 }
154
155 void
156 Add_symbols::run(Workqueue*)
157 {
158 this->object_->add_symbols(this->symtab_, this->sd_);
159 }
160
161 } // End namespace gold.