+#define SWAP(type, a, b) \
+ do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
+
+/* Compare two hash table entries */
+
+static int eq_sym (const void *a, const void *b)
+{
+ const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
+ const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
+
+ return !strcmp (as->name, bs->name);
+}
+
+/* Hash a symbol */
+
+static hashval_t hash_sym (const void *a)
+{
+ const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
+
+ return htab_hash_string (as->name);
+}
+
+/* Determine how strong a symbol is */
+
+static int symbol_strength (struct ld_plugin_symbol *s)
+{
+ switch (s->def)
+ {
+ case LDPK_UNDEF:
+ case LDPK_WEAKUNDEF:
+ return 0;
+ case LDPK_WEAKDEF:
+ return 1;
+ default:
+ return 2;
+ }
+}
+
+/* In the ld -r case we can get dups in the LTO symbol tables, where
+ the same symbol can have different resolutions (e.g. undefined and defined).
+
+ We have to keep that in the LTO symbol tables, but the dups confuse
+ gold and then finally gcc by supplying incorrect resolutions.
+
+ Problem is that the main gold symbol table doesn't know about subids
+ and does not distingush the same symbols in different states.
+
+ So we drop duplicates from the linker visible symbol table
+ and keep them in a private table. Then later do own symbol
+ resolution for the duplicated based on the results for the
+ originals.
+
+ Then when writing out the resolution file readd the dropped symbols.
+
+ XXX how to handle common? */
+
+static void
+resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
+{
+ htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
+ int i;
+ int out;
+ int outlen;
+
+ outlen = t->nsyms;
+ conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
+ conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
+
+ /* Move all duplicate symbols into the auxillary conflicts table. */
+ out = 0;
+ for (i = 0; i < t->nsyms; i++)
+ {
+ struct ld_plugin_symbol *s = &t->syms[i];
+ struct sym_aux *aux = &t->aux[i];
+ void **slot;
+
+ slot = htab_find_slot (symtab, s, INSERT);
+ if (*slot != NULL)
+ {
+ int cnf;
+ struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
+ struct sym_aux *orig_aux = &t->aux[orig - t->syms];
+
+ /* Always let the linker resolve the strongest symbol */
+ if (symbol_strength (orig) < symbol_strength (s))
+ {
+ SWAP (struct ld_plugin_symbol, *orig, *s);
+ SWAP (uint32_t, orig_aux->slot, aux->slot);
+ SWAP (unsigned, orig_aux->id, aux->id);
+ /* Don't swap conflict chain pointer */
+ }
+
+ /* Move current symbol into the conflicts table */
+ cnf = conflicts->nsyms++;
+ conflicts->syms[cnf] = *s;
+ conflicts->aux[cnf] = *aux;
+ aux = &conflicts->aux[cnf];
+
+ /* Update conflicts chain of the original symbol */
+ aux->next_conflict = orig_aux->next_conflict;
+ orig_aux->next_conflict = cnf;
+
+ continue;
+ }
+
+ /* Remove previous duplicates in the main table */
+ if (out < i)
+ {
+ t->syms[out] = *s;
+ t->aux[out] = *aux;
+ }
+
+ /* Put original into the hash table */
+ *slot = &t->syms[out];
+ out++;
+ }
+
+ assert (conflicts->nsyms <= outlen);
+ assert (conflicts->nsyms + out == t->nsyms);
+
+ t->nsyms = out;
+ htab_delete (symtab);
+}
+
+/* Process one section of an object file. */
+
+static int
+process_symtab (void *data, const char *name, off_t offset, off_t length)
+{
+ struct plugin_objfile *obj = (struct plugin_objfile *)data;
+ char *s;
+ char *secdata;
+
+ if (strncmp (name, LTO_SECTION_PREFIX, LTO_SECTION_PREFIX_LEN) != 0)
+ return 1;
+
+ s = strrchr (name, '.');
+ if (s)
+ sscanf (s, ".%x", &obj->out->id);
+ secdata = xmalloc (length);
+ offset += obj->file->offset;
+ if (offset != lseek (obj->file->fd, offset, SEEK_SET)
+ || length != read (obj->file->fd, secdata, length))
+ {
+ if (message)
+ message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
+ /* Force claim_file_handler to abandon this file. */
+ obj->found = 0;
+ free (secdata);
+ return 0;
+ }
+
+ translate (secdata, secdata + length, obj->out);
+ obj->found++;
+ free (secdata);
+ return 1;
+}
+