+ bool saw_backslash = false;
+ size_t len = pattern->length();
+ size_t j = 0;
+ for (size_t i = 0; i < len; ++i)
+ {
+ if (saw_backslash)
+ saw_backslash = false;
+ else
+ {
+ switch ((*pattern)[i])
+ {
+ case '?': case '[': case '*':
+ return true;
+ case '\\':
+ saw_backslash = true;
+ continue;
+ default:
+ break;
+ }
+ }
+
+ if (i != j)
+ (*pattern)[j] = (*pattern)[i];
+ ++j;
+ }
+ return false;
+}
+
+// Add an exact match for MATCH to *PE. The result of the match is
+// V/IS_GLOBAL.
+
+void
+Version_script_info::add_exact_match(const std::string& match,
+ const Version_tree* v, bool is_global,
+ const Version_expression* ve,
+ Exact* pe)
+{
+ std::pair<Exact::iterator, bool> ins =
+ pe->insert(std::make_pair(match, Version_tree_match(v, is_global, ve)));
+ if (ins.second)
+ {
+ // This is the first time we have seen this match.
+ return;
+ }
+
+ Version_tree_match& vtm(ins.first->second);
+ if (vtm.real->tag != v->tag)
+ {
+ // This is an ambiguous match. We still return the
+ // first version that we found in the script, but we
+ // record the new version to issue a warning if we
+ // wind up looking up this symbol.
+ if (vtm.ambiguous == NULL)
+ vtm.ambiguous = v;
+ }
+ else if (is_global != vtm.is_global)
+ {
+ // We have a match for both the global and local entries for a
+ // version tag. That's got to be wrong.
+ gold_error(_("'%s' appears as both a global and a local symbol "
+ "for version '%s' in script"),
+ match.c_str(), v->tag.c_str());
+ }
+}
+
+// Build fast lookup information for EXPLIST and store it in LOOKUP.
+// All matches go to V, and IS_GLOBAL is true if they are global
+// matches.
+
+void
+Version_script_info::build_expression_list_lookup(
+ const Version_expression_list* explist,
+ const Version_tree* v,
+ bool is_global)
+{
+ if (explist == NULL)
+ return;
+ size_t size = explist->expressions.size();
+ for (size_t i = 0; i < size; ++i)
+ {
+ const Version_expression& exp(explist->expressions[i]);
+
+ if (exp.pattern.length() == 1 && exp.pattern[0] == '*')
+ {
+ if (this->default_version_ != NULL
+ && this->default_version_->tag != v->tag)
+ gold_warning(_("wildcard match appears in both version '%s' "
+ "and '%s' in script"),
+ this->default_version_->tag.c_str(), v->tag.c_str());
+ else if (this->default_version_ != NULL
+ && this->default_is_global_ != is_global)
+ gold_error(_("wildcard match appears as both global and local "
+ "in version '%s' in script"),
+ v->tag.c_str());
+ this->default_version_ = v;
+ this->default_is_global_ = is_global;
+ continue;
+ }
+
+ std::string pattern = exp.pattern;
+ if (!exp.exact_match)
+ {
+ if (this->unquote(&pattern))
+ {
+ this->globs_.push_back(Glob(&exp, v, is_global));
+ continue;
+ }
+ }
+
+ if (this->exact_[exp.language] == NULL)
+ this->exact_[exp.language] = new Exact();
+ this->add_exact_match(pattern, v, is_global, &exp,
+ this->exact_[exp.language]);
+ }
+}
+
+// Return the name to match given a name, a language code, and two
+// lazy demanglers.
+
+const char*
+Version_script_info::get_name_to_match(const char* name,
+ int language,
+ Lazy_demangler* cpp_demangler,
+ Lazy_demangler* java_demangler) const
+{
+ switch (language)
+ {
+ case LANGUAGE_C:
+ return name;
+ case LANGUAGE_CXX:
+ return cpp_demangler->get();
+ case LANGUAGE_JAVA:
+ return java_demangler->get();
+ default:
+ gold_unreachable();
+ }
+}
+
+// Look up SYMBOL_NAME in the list of versions. Return true if the
+// symbol is found, false if not. If the symbol is found, then if
+// PVERSION is not NULL, set *PVERSION to the version tag, and if
+// P_IS_GLOBAL is not NULL, set *P_IS_GLOBAL according to whether the
+// symbol is global or not.
+
+bool
+Version_script_info::get_symbol_version(const char* symbol_name,
+ std::string* pversion,
+ bool* p_is_global) const
+{
+ Lazy_demangler cpp_demangled_name(symbol_name, DMGL_ANSI | DMGL_PARAMS);
+ Lazy_demangler java_demangled_name(symbol_name,
+ DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
+
+ gold_assert(this->is_finalized_);
+ for (int i = 0; i < LANGUAGE_COUNT; ++i)
+ {
+ Exact* exact = this->exact_[i];
+ if (exact == NULL)
+ continue;
+
+ const char* name_to_match = this->get_name_to_match(symbol_name, i,
+ &cpp_demangled_name,
+ &java_demangled_name);
+ if (name_to_match == NULL)
+ {
+ // If the name can not be demangled, the GNU linker goes
+ // ahead and tries to match it anyhow. That does not
+ // make sense to me and I have not implemented it.
+ continue;
+ }
+
+ Exact::const_iterator pe = exact->find(name_to_match);
+ if (pe != exact->end())
+ {
+ const Version_tree_match& vtm(pe->second);
+ if (vtm.ambiguous != NULL)
+ gold_warning(_("using '%s' as version for '%s' which is also "
+ "named in version '%s' in script"),
+ vtm.real->tag.c_str(), name_to_match,
+ vtm.ambiguous->tag.c_str());
+
+ if (pversion != NULL)
+ *pversion = vtm.real->tag;
+ if (p_is_global != NULL)
+ *p_is_global = vtm.is_global;
+
+ // If we are using --no-undefined-version, and this is a
+ // global symbol, we have to record that we have found this
+ // symbol, so that we don't warn about it. We have to do
+ // this now, because otherwise we have no way to get from a
+ // non-C language back to the demangled name that we
+ // matched.
+ if (p_is_global != NULL && vtm.is_global)
+ vtm.expression->was_matched_by_symbol = true;
+
+ return true;
+ }
+ }
+
+ // Look through the glob patterns in reverse order.
+
+ for (Globs::const_reverse_iterator p = this->globs_.rbegin();
+ p != this->globs_.rend();
+ ++p)
+ {
+ int language = p->expression->language;
+ const char* name_to_match = this->get_name_to_match(symbol_name,
+ language,
+ &cpp_demangled_name,
+ &java_demangled_name);
+ if (name_to_match == NULL)
+ continue;
+
+ if (fnmatch(p->expression->pattern.c_str(), name_to_match,
+ FNM_NOESCAPE) == 0)
+ {
+ if (pversion != NULL)
+ *pversion = p->version->tag;
+ if (p_is_global != NULL)
+ *p_is_global = p->is_global;
+ return true;
+ }
+ }
+
+ // Finally, there may be a wildcard.
+ if (this->default_version_ != NULL)
+ {
+ if (pversion != NULL)
+ *pversion = this->default_version_->tag;
+ if (p_is_global != NULL)
+ *p_is_global = this->default_is_global_;
+ return true;
+ }
+
+ return false;
+}
+
+// Give an error if any exact symbol names (not wildcards) appear in a
+// version script, but there is no such symbol.
+
+void
+Version_script_info::check_unmatched_names(const Symbol_table* symtab) const
+{
+ for (size_t i = 0; i < this->version_trees_.size(); ++i)
+ {
+ const Version_tree* vt = this->version_trees_[i];
+ if (vt->global == NULL)
+ continue;
+ for (size_t j = 0; j < vt->global->expressions.size(); ++j)
+ {
+ const Version_expression& expression(vt->global->expressions[j]);
+
+ // Ignore cases where we used the version because we saw a
+ // symbol that we looked up. Note that
+ // WAS_MATCHED_BY_SYMBOL will be true even if the symbol was
+ // not a definition. That's OK as in that case we most
+ // likely gave an undefined symbol error anyhow.
+ if (expression.was_matched_by_symbol)
+ continue;
+
+ // Just ignore names which are in languages other than C.
+ // We have no way to look them up in the symbol table.
+ if (expression.language != LANGUAGE_C)
+ continue;
+
+ // Remove backslash quoting, and ignore wildcard patterns.
+ std::string pattern = expression.pattern;
+ if (!expression.exact_match)
+ {
+ if (this->unquote(&pattern))
+ continue;
+ }
+
+ if (symtab->lookup(pattern.c_str(), vt->tag.c_str()) == NULL)
+ gold_error(_("version script assignment of %s to symbol %s "
+ "failed: symbol not defined"),
+ vt->tag.c_str(), pattern.c_str());
+ }
+ }
+}
+
+struct Version_dependency_list*
+Version_script_info::allocate_dependency_list()
+{
+ dependency_lists_.push_back(new Version_dependency_list);
+ return dependency_lists_.back();
+}
+
+struct Version_expression_list*
+Version_script_info::allocate_expression_list()
+{
+ expression_lists_.push_back(new Version_expression_list);
+ return expression_lists_.back();
+}
+
+struct Version_tree*
+Version_script_info::allocate_version_tree()
+{
+ version_trees_.push_back(new Version_tree);
+ return version_trees_.back();
+}
+
+// Print for debugging.
+
+void
+Version_script_info::print(FILE* f) const
+{
+ if (this->empty())
+ return;
+
+ fprintf(f, "VERSION {");
+
+ for (size_t i = 0; i < this->version_trees_.size(); ++i)
+ {
+ const Version_tree* vt = this->version_trees_[i];
+
+ if (vt->tag.empty())
+ fprintf(f, " {\n");
+ else
+ fprintf(f, " %s {\n", vt->tag.c_str());
+
+ if (vt->global != NULL)
+ {
+ fprintf(f, " global :\n");
+ this->print_expression_list(f, vt->global);
+ }
+
+ if (vt->local != NULL)
+ {
+ fprintf(f, " local :\n");
+ this->print_expression_list(f, vt->local);
+ }
+
+ fprintf(f, " }");
+ if (vt->dependencies != NULL)
+ {
+ const Version_dependency_list* deps = vt->dependencies;
+ for (size_t j = 0; j < deps->dependencies.size(); ++j)
+ {
+ if (j < deps->dependencies.size() - 1)
+ fprintf(f, "\n");
+ fprintf(f, " %s", deps->dependencies[j].c_str());
+ }
+ }
+ fprintf(f, ";\n");
+ }
+
+ fprintf(f, "}\n");
+}
+
+void
+Version_script_info::print_expression_list(
+ FILE* f,
+ const Version_expression_list* vel) const
+{
+ Version_script_info::Language current_language = LANGUAGE_C;
+ for (size_t i = 0; i < vel->expressions.size(); ++i)
+ {
+ const Version_expression& ve(vel->expressions[i]);
+
+ if (ve.language != current_language)
+ {
+ if (current_language != LANGUAGE_C)
+ fprintf(f, " }\n");
+ switch (ve.language)
+ {
+ case LANGUAGE_C:
+ break;
+ case LANGUAGE_CXX:
+ fprintf(f, " extern \"C++\" {\n");
+ break;
+ case LANGUAGE_JAVA:
+ fprintf(f, " extern \"Java\" {\n");
+ break;
+ default:
+ gold_unreachable();
+ }
+ current_language = ve.language;
+ }
+
+ fprintf(f, " ");
+ if (current_language != LANGUAGE_C)
+ fprintf(f, " ");
+
+ if (ve.exact_match)
+ fprintf(f, "\"");
+ fprintf(f, "%s", ve.pattern.c_str());
+ if (ve.exact_match)
+ fprintf(f, "\"");
+
+ fprintf(f, "\n");
+ }
+
+ if (current_language != LANGUAGE_C)
+ fprintf(f, " }\n");
+}
+
+} // End namespace gold.
+
+// The remaining functions are extern "C", so it's clearer to not put
+// them in namespace gold.
+
+using namespace gold;
+
+// This function is called by the bison parser to return the next
+// token.
+
+extern "C" int
+yylex(YYSTYPE* lvalp, void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ const Token* token = closure->next_token();
+ switch (token->classification())
+ {
+ default:
+ gold_unreachable();
+
+ case Token::TOKEN_INVALID:
+ yyerror(closurev, "invalid character");
+ return 0;
+
+ case Token::TOKEN_EOF:
+ return 0;
+
+ case Token::TOKEN_STRING:
+ {
+ // This is either a keyword or a STRING.
+ size_t len;
+ const char* str = token->string_value(&len);
+ int parsecode = 0;
+ switch (closure->lex_mode())
+ {
+ case Lex::LINKER_SCRIPT:
+ parsecode = script_keywords.keyword_to_parsecode(str, len);
+ break;
+ case Lex::VERSION_SCRIPT:
+ parsecode = version_script_keywords.keyword_to_parsecode(str, len);
+ break;
+ case Lex::DYNAMIC_LIST:
+ parsecode = dynamic_list_keywords.keyword_to_parsecode(str, len);
+ break;
+ default:
+ break;
+ }
+ if (parsecode != 0)
+ return parsecode;
+ lvalp->string.value = str;
+ lvalp->string.length = len;
+ return STRING;
+ }
+
+ case Token::TOKEN_QUOTED_STRING:
+ lvalp->string.value = token->string_value(&lvalp->string.length);
+ return QUOTED_STRING;
+
+ case Token::TOKEN_OPERATOR:
+ return token->operator_value();
+
+ case Token::TOKEN_INTEGER:
+ lvalp->integer = token->integer_value();
+ return INTEGER;
+ }
+}
+
+// This function is called by the bison parser to report an error.
+
+extern "C" void
+yyerror(void* closurev, const char* message)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
+ closure->charpos(), message);
+}
+
+// Called by the bison parser to add an external symbol to the link.
+
+extern "C" void
+script_add_extern(void* closurev, const char* name, size_t length)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->script_options()->add_symbol_reference(name, length);
+}
+
+// Called by the bison parser to add a file to the link.
+
+extern "C" void
+script_add_file(void* closurev, const char* name, size_t length)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+
+ // If this is an absolute path, and we found the script in the
+ // sysroot, then we want to prepend the sysroot to the file name.
+ // For example, this is how we handle a cross link to the x86_64
+ // libc.so, which refers to /lib/libc.so.6.
+ std::string name_string(name, length);
+ const char* extra_search_path = ".";
+ std::string script_directory;
+ if (IS_ABSOLUTE_PATH(name_string.c_str()))
+ {
+ if (closure->is_in_sysroot())
+ {
+ const std::string& sysroot(parameters->options().sysroot());
+ gold_assert(!sysroot.empty());
+ name_string = sysroot + name_string;
+ }
+ }
+ else
+ {
+ // In addition to checking the normal library search path, we
+ // also want to check in the script-directory.
+ const char* slash = strrchr(closure->filename(), '/');
+ if (slash != NULL)
+ {
+ script_directory.assign(closure->filename(),
+ slash - closure->filename() + 1);
+ extra_search_path = script_directory.c_str();
+ }
+ }
+
+ Input_file_argument file(name_string.c_str(),
+ Input_file_argument::INPUT_FILE_TYPE_FILE,
+ extra_search_path, false,
+ closure->position_dependent_options());
+ Input_argument& arg = closure->inputs()->add_file(file);
+ arg.set_script_info(closure->script_info());
+}
+
+// Called by the bison parser to add a library to the link.
+
+extern "C" void
+script_add_library(void* closurev, const char* name, size_t length)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ std::string name_string(name, length);
+
+ if (name_string[0] != 'l')
+ gold_error(_("library name must be prefixed with -l"));
+
+ Input_file_argument file(name_string.c_str() + 1,
+ Input_file_argument::INPUT_FILE_TYPE_LIBRARY,
+ "", false,
+ closure->position_dependent_options());
+ Input_argument& arg = closure->inputs()->add_file(file);
+ arg.set_script_info(closure->script_info());
+}
+
+// Called by the bison parser to start a group. If we are already in
+// a group, that means that this script was invoked within a
+// --start-group --end-group sequence on the command line, or that
+// this script was found in a GROUP of another script. In that case,
+// we simply continue the existing group, rather than starting a new
+// one. It is possible to construct a case in which this will do
+// something other than what would happen if we did a recursive group,
+// but it's hard to imagine why the different behaviour would be
+// useful for a real program. Avoiding recursive groups is simpler
+// and more efficient.
+
+extern "C" void
+script_start_group(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (!closure->in_group())
+ closure->inputs()->start_group();
+}
+
+// Called by the bison parser at the end of a group.
+
+extern "C" void
+script_end_group(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (!closure->in_group())
+ closure->inputs()->end_group();
+}
+
+// Called by the bison parser to start an AS_NEEDED list.
+
+extern "C" void
+script_start_as_needed(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->position_dependent_options().set_as_needed(true);
+}
+
+// Called by the bison parser at the end of an AS_NEEDED list.
+
+extern "C" void
+script_end_as_needed(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->position_dependent_options().set_as_needed(false);
+}
+
+// Called by the bison parser to set the entry symbol.
+
+extern "C" void
+script_set_entry(void* closurev, const char* entry, size_t length)
+{
+ // We'll parse this exactly the same as --entry=ENTRY on the commandline
+ // TODO(csilvers): FIXME -- call set_entry directly.
+ std::string arg("--entry=");
+ arg.append(entry, length);
+ script_parse_option(closurev, arg.c_str(), arg.size());
+}
+
+// Called by the bison parser to set whether to define common symbols.
+
+extern "C" void
+script_set_common_allocation(void* closurev, int set)
+{
+ const char* arg = set != 0 ? "--define-common" : "--no-define-common";
+ script_parse_option(closurev, arg, strlen(arg));
+}
+
+// Called by the bison parser to refer to a symbol.
+
+extern "C" Expression*
+script_symbol(void* closurev, const char* name, size_t length)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (length != 1 || name[0] != '.')
+ closure->script_options()->add_symbol_reference(name, length);
+ return script_exp_string(name, length);
+}
+
+// Called by the bison parser to define a symbol.
+
+extern "C" void
+script_set_symbol(void* closurev, const char* name, size_t length,
+ Expression* value, int providei, int hiddeni)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ const bool provide = providei != 0;
+ const bool hidden = hiddeni != 0;
+ closure->script_options()->add_symbol_assignment(name, length,
+ closure->parsing_defsym(),
+ value, provide, hidden);
+ closure->clear_skip_on_incompatible_target();
+}
+
+// Called by the bison parser to add an assertion.
+
+extern "C" void
+script_add_assertion(void* closurev, Expression* check, const char* message,
+ size_t messagelen)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->script_options()->add_assertion(check, message, messagelen);
+ closure->clear_skip_on_incompatible_target();
+}
+
+// Called by the bison parser to parse an OPTION.
+
+extern "C" void
+script_parse_option(void* closurev, const char* option, size_t length)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ // We treat the option as a single command-line option, even if
+ // it has internal whitespace.
+ if (closure->command_line() == NULL)
+ {
+ // There are some options that we could handle here--e.g.,
+ // -lLIBRARY. Should we bother?
+ gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
+ " for scripts specified via -T/--script"),
+ closure->filename(), closure->lineno(), closure->charpos());
+ }
+ else
+ {
+ bool past_a_double_dash_option = false;
+ const char* mutable_option = strndup(option, length);
+ gold_assert(mutable_option != NULL);
+ closure->command_line()->process_one_option(1, &mutable_option, 0,
+ &past_a_double_dash_option);
+ // The General_options class will quite possibly store a pointer
+ // into mutable_option, so we can't free it. In cases the class
+ // does not store such a pointer, this is a memory leak. Alas. :(
+ }
+ closure->clear_skip_on_incompatible_target();
+}
+
+// Called by the bison parser to handle OUTPUT_FORMAT. OUTPUT_FORMAT
+// takes either one or three arguments. In the three argument case,
+// the format depends on the endianness option, which we don't
+// currently support (FIXME). If we see an OUTPUT_FORMAT for the
+// wrong format, then we want to search for a new file. Returning 0
+// here will cause the parser to immediately abort.
+
+extern "C" int
+script_check_output_format(void* closurev,
+ const char* default_name, size_t default_length,
+ const char*, size_t, const char*, size_t)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ std::string name(default_name, default_length);
+ Target* target = select_target_by_bfd_name(name.c_str());
+ if (target == NULL || !parameters->is_compatible_target(target))
+ {
+ if (closure->skip_on_incompatible_target())
+ {
+ closure->set_found_incompatible_target();
+ return 0;
+ }
+ // FIXME: Should we warn about the unknown target?
+ }
+ return 1;
+}
+
+// Called by the bison parser to handle TARGET.
+
+extern "C" void
+script_set_target(void* closurev, const char* target, size_t len)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ std::string s(target, len);
+ General_options::Object_format format_enum;
+ format_enum = General_options::string_to_object_format(s.c_str());
+ closure->position_dependent_options().set_format_enum(format_enum);
+}
+
+// Called by the bison parser to handle SEARCH_DIR. This is handled
+// exactly like a -L option.
+
+extern "C" void
+script_add_search_dir(void* closurev, const char* option, size_t length)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (closure->command_line() == NULL)
+ gold_warning(_("%s:%d:%d: ignoring SEARCH_DIR; SEARCH_DIR is only valid"
+ " for scripts specified via -T/--script"),
+ closure->filename(), closure->lineno(), closure->charpos());
+ else if (!closure->command_line()->options().nostdlib())
+ {
+ std::string s = "-L" + std::string(option, length);
+ script_parse_option(closurev, s.c_str(), s.size());
+ }
+}
+
+/* Called by the bison parser to push the lexer into expression
+ mode. */
+
+extern "C" void
+script_push_lex_into_expression_mode(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->push_lex_mode(Lex::EXPRESSION);
+}
+
+/* Called by the bison parser to push the lexer into version
+ mode. */
+
+extern "C" void
+script_push_lex_into_version_mode(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (closure->version_script()->is_finalized())
+ gold_error(_("%s:%d:%d: invalid use of VERSION in input file"),
+ closure->filename(), closure->lineno(), closure->charpos());
+ closure->push_lex_mode(Lex::VERSION_SCRIPT);
+}
+
+/* Called by the bison parser to pop the lexer mode. */
+
+extern "C" void
+script_pop_lex_mode(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->pop_lex_mode();
+}
+
+// Register an entire version node. For example:
+//
+// GLIBC_2.1 {
+// global: foo;
+// } GLIBC_2.0;
+//
+// - tag is "GLIBC_2.1"
+// - tree contains the information "global: foo"
+// - deps contains "GLIBC_2.0"
+
+extern "C" void
+script_register_vers_node(void*,
+ const char* tag,
+ int taglen,
+ struct Version_tree* tree,
+ struct Version_dependency_list* deps)
+{
+ gold_assert(tree != NULL);
+ tree->dependencies = deps;
+ if (tag != NULL)
+ tree->tag = std::string(tag, taglen);
+}
+
+// Add a dependencies to the list of existing dependencies, if any,
+// and return the expanded list.
+
+extern "C" struct Version_dependency_list*
+script_add_vers_depend(void* closurev,
+ struct Version_dependency_list* all_deps,
+ const char* depend_to_add, int deplen)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (all_deps == NULL)
+ all_deps = closure->version_script()->allocate_dependency_list();
+ all_deps->dependencies.push_back(std::string(depend_to_add, deplen));
+ return all_deps;
+}
+
+// Add a pattern expression to an existing list of expressions, if any.
+
+extern "C" struct Version_expression_list*
+script_new_vers_pattern(void* closurev,
+ struct Version_expression_list* expressions,
+ const char* pattern, int patlen, int exact_match)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (expressions == NULL)
+ expressions = closure->version_script()->allocate_expression_list();
+ expressions->expressions.push_back(
+ Version_expression(std::string(pattern, patlen),
+ closure->get_current_language(),
+ static_cast<bool>(exact_match)));
+ return expressions;
+}
+
+// Attaches b to the end of a, and clears b. So a = a + b and b = {}.
+
+extern "C" struct Version_expression_list*
+script_merge_expressions(struct Version_expression_list* a,
+ struct Version_expression_list* b)
+{
+ a->expressions.insert(a->expressions.end(),
+ b->expressions.begin(), b->expressions.end());
+ // We could delete b and remove it from expressions_lists_, but
+ // that's a lot of work. This works just as well.
+ b->expressions.clear();
+ return a;
+}
+
+// Combine the global and local expressions into a a Version_tree.
+
+extern "C" struct Version_tree*
+script_new_vers_node(void* closurev,
+ struct Version_expression_list* global,
+ struct Version_expression_list* local)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ Version_tree* tree = closure->version_script()->allocate_version_tree();
+ tree->global = global;
+ tree->local = local;
+ return tree;
+}
+
+// Handle a transition in language, such as at the
+// start or end of 'extern "C++"'
+
+extern "C" void
+version_script_push_lang(void* closurev, const char* lang, int langlen)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ std::string language(lang, langlen);
+ Version_script_info::Language code;
+ if (language.empty() || language == "C")
+ code = Version_script_info::LANGUAGE_C;
+ else if (language == "C++")
+ code = Version_script_info::LANGUAGE_CXX;
+ else if (language == "Java")
+ code = Version_script_info::LANGUAGE_JAVA;
+ else
+ {
+ char* buf = new char[langlen + 100];
+ snprintf(buf, langlen + 100,
+ _("unrecognized version script language '%s'"),
+ language.c_str());
+ yyerror(closurev, buf);
+ delete[] buf;
+ code = Version_script_info::LANGUAGE_C;
+ }
+ closure->push_language(code);
+}
+
+extern "C" void
+version_script_pop_lang(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->pop_language();
+}
+
+// Called by the bison parser to start a SECTIONS clause.
+
+extern "C" void
+script_start_sections(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->script_options()->script_sections()->start_sections();
+ closure->clear_skip_on_incompatible_target();
+}
+
+// Called by the bison parser to finish a SECTIONS clause.
+
+extern "C" void
+script_finish_sections(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->script_options()->script_sections()->finish_sections();
+}
+
+// Start processing entries for an output section.
+
+extern "C" void
+script_start_output_section(void* closurev, const char* name, size_t namelen,
+ const struct Parser_output_section_header* header)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->script_options()->script_sections()->start_output_section(name,
+ namelen,
+ header);
+}
+
+// Finish processing entries for an output section.
+
+extern "C" void
+script_finish_output_section(void* closurev,
+ const struct Parser_output_section_trailer* trail)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->script_options()->script_sections()->finish_output_section(trail);
+}
+
+// Add a data item (e.g., "WORD (0)") to the current output section.
+
+extern "C" void
+script_add_data(void* closurev, int data_token, Expression* val)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ int size;
+ bool is_signed = true;
+ switch (data_token)
+ {
+ case QUAD:
+ size = 8;
+ is_signed = false;
+ break;
+ case SQUAD:
+ size = 8;
+ break;
+ case LONG:
+ size = 4;
+ break;
+ case SHORT:
+ size = 2;
+ break;
+ case BYTE:
+ size = 1;
+ break;
+ default:
+ gold_unreachable();
+ }
+ closure->script_options()->script_sections()->add_data(size, is_signed, val);
+}
+
+// Add a clause setting the fill value to the current output section.
+
+extern "C" void
+script_add_fill(void* closurev, Expression* val)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ closure->script_options()->script_sections()->add_fill(val);
+}
+
+// Add a new input section specification to the current output
+// section.
+
+extern "C" void
+script_add_input_section(void* closurev,
+ const struct Input_section_spec* spec,
+ int keepi)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ bool keep = keepi != 0;
+ closure->script_options()->script_sections()->add_input_section(spec, keep);
+}
+
+// When we see DATA_SEGMENT_ALIGN we record that following output
+// sections may be relro.
+
+extern "C" void
+script_data_segment_align(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (!closure->script_options()->saw_sections_clause())
+ gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
+ closure->filename(), closure->lineno(), closure->charpos());
+ else
+ closure->script_options()->script_sections()->data_segment_align();
+}
+
+// When we see DATA_SEGMENT_RELRO_END we know that all output sections
+// since DATA_SEGMENT_ALIGN should be relro.
+
+extern "C" void
+script_data_segment_relro_end(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (!closure->script_options()->saw_sections_clause())
+ gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
+ closure->filename(), closure->lineno(), closure->charpos());
+ else
+ closure->script_options()->script_sections()->data_segment_relro_end();
+}
+
+// Create a new list of string/sort pairs.
+
+extern "C" String_sort_list_ptr
+script_new_string_sort_list(const struct Wildcard_section* string_sort)
+{
+ return new String_sort_list(1, *string_sort);
+}
+
+// Add an entry to a list of string/sort pairs. The way the parser
+// works permits us to simply modify the first parameter, rather than
+// copy the vector.
+
+extern "C" String_sort_list_ptr
+script_string_sort_list_add(String_sort_list_ptr pv,
+ const struct Wildcard_section* string_sort)
+{
+ if (pv == NULL)
+ return script_new_string_sort_list(string_sort);
+ else
+ {
+ pv->push_back(*string_sort);
+ return pv;
+ }
+}
+
+// Create a new list of strings.
+
+extern "C" String_list_ptr
+script_new_string_list(const char* str, size_t len)
+{
+ return new String_list(1, std::string(str, len));
+}
+
+// Add an element to a list of strings. The way the parser works
+// permits us to simply modify the first parameter, rather than copy
+// the vector.
+
+extern "C" String_list_ptr
+script_string_list_push_back(String_list_ptr pv, const char* str, size_t len)
+{
+ if (pv == NULL)
+ return script_new_string_list(str, len);
+ else
+ {
+ pv->push_back(std::string(str, len));
+ return pv;
+ }
+}
+
+// Concatenate two string lists. Either or both may be NULL. The way
+// the parser works permits us to modify the parameters, rather than
+// copy the vector.
+
+extern "C" String_list_ptr
+script_string_list_append(String_list_ptr pv1, String_list_ptr pv2)
+{
+ if (pv1 == NULL)
+ return pv2;
+ if (pv2 == NULL)
+ return pv1;
+ pv1->insert(pv1->end(), pv2->begin(), pv2->end());
+ return pv1;
+}
+
+// Add a new program header.
+
+extern "C" void
+script_add_phdr(void* closurev, const char* name, size_t namelen,
+ unsigned int type, const Phdr_info* info)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ bool includes_filehdr = info->includes_filehdr != 0;
+ bool includes_phdrs = info->includes_phdrs != 0;
+ bool is_flags_valid = info->is_flags_valid != 0;
+ Script_sections* ss = closure->script_options()->script_sections();
+ ss->add_phdr(name, namelen, type, includes_filehdr, includes_phdrs,
+ is_flags_valid, info->flags, info->load_address);
+ closure->clear_skip_on_incompatible_target();
+}
+
+// Convert a program header string to a type.
+
+#define PHDR_TYPE(NAME) { #NAME, sizeof(#NAME) - 1, elfcpp::NAME }
+
+static struct
+{
+ const char* name;
+ size_t namelen;
+ unsigned int val;
+} phdr_type_names[] =
+{
+ PHDR_TYPE(PT_NULL),
+ PHDR_TYPE(PT_LOAD),
+ PHDR_TYPE(PT_DYNAMIC),
+ PHDR_TYPE(PT_INTERP),
+ PHDR_TYPE(PT_NOTE),
+ PHDR_TYPE(PT_SHLIB),
+ PHDR_TYPE(PT_PHDR),
+ PHDR_TYPE(PT_TLS),
+ PHDR_TYPE(PT_GNU_EH_FRAME),
+ PHDR_TYPE(PT_GNU_STACK),
+ PHDR_TYPE(PT_GNU_RELRO)
+};
+
+extern "C" unsigned int
+script_phdr_string_to_type(void* closurev, const char* name, size_t namelen)
+{
+ for (unsigned int i = 0;
+ i < sizeof(phdr_type_names) / sizeof(phdr_type_names[0]);
+ ++i)
+ if (namelen == phdr_type_names[i].namelen
+ && strncmp(name, phdr_type_names[i].name, namelen) == 0)
+ return phdr_type_names[i].val;
+ yyerror(closurev, _("unknown PHDR type (try integer)"));
+ return elfcpp::PT_NULL;
+}
+
+extern "C" void
+script_saw_segment_start_expression(void* closurev)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ Script_sections* ss = closure->script_options()->script_sections();
+ ss->set_saw_segment_start_expression(true);
+}
+
+extern "C" void
+script_set_section_region(void* closurev, const char* name, size_t namelen,
+ int set_vma)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ if (!closure->script_options()->saw_sections_clause())
+ {
+ gold_error(_("%s:%d:%d: MEMORY region '%.*s' referred to outside of "
+ "SECTIONS clause"),
+ closure->filename(), closure->lineno(), closure->charpos(),
+ static_cast<int>(namelen), name);
+ return;
+ }
+
+ Script_sections* ss = closure->script_options()->script_sections();
+ Memory_region* mr = ss->find_memory_region(name, namelen);
+ if (mr == NULL)
+ {
+ gold_error(_("%s:%d:%d: MEMORY region '%.*s' not declared"),
+ closure->filename(), closure->lineno(), closure->charpos(),
+ static_cast<int>(namelen), name);
+ return;
+ }
+
+ ss->set_memory_region(mr, set_vma);
+}
+
+extern "C" void
+script_add_memory(void* closurev, const char* name, size_t namelen,
+ unsigned int attrs, Expression* origin, Expression* length)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ Script_sections* ss = closure->script_options()->script_sections();
+ ss->add_memory_region(name, namelen, attrs, origin, length);
+}
+
+extern "C" unsigned int
+script_parse_memory_attr(void* closurev, const char* attrs, size_t attrlen,
+ int invert)
+{
+ int attributes = 0;
+
+ while (attrlen--)
+ switch (*attrs++)
+ {
+ case 'R':
+ case 'r':
+ attributes |= MEM_READABLE; break;
+ case 'W':
+ case 'w':
+ attributes |= MEM_READABLE | MEM_WRITEABLE; break;
+ case 'X':
+ case 'x':
+ attributes |= MEM_EXECUTABLE; break;
+ case 'A':
+ case 'a':
+ attributes |= MEM_ALLOCATABLE; break;
+ case 'I':
+ case 'i':
+ case 'L':
+ case 'l':
+ attributes |= MEM_INITIALIZED; break;
+ default:
+ yyerror(closurev, _("unknown MEMORY attribute"));
+ }
+
+ if (invert)
+ attributes = (~ attributes) & MEM_ATTR_MASK;
+
+ return attributes;
+}
+
+extern "C" void
+script_include_directive(int first_token, void* closurev,
+ const char* filename, size_t length)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ std::string name(filename, length);
+ Command_line* cmdline = closure->command_line();
+ read_script_file(name.c_str(), cmdline, &cmdline->script_options(),
+ first_token, Lex::LINKER_SCRIPT);
+}
+
+// Functions for memory regions.
+
+extern "C" Expression*
+script_exp_function_origin(void* closurev, const char* name, size_t namelen)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ Script_sections* ss = closure->script_options()->script_sections();
+ Expression* origin = ss->find_memory_region_origin(name, namelen);
+
+ if (origin == NULL)
+ {
+ gold_error(_("undefined memory region '%s' referenced "
+ "in ORIGIN expression"),
+ name);
+ // Create a dummy expression to prevent crashes later on.
+ origin = script_exp_integer(0);
+ }
+
+ return origin;
+}
+
+extern "C" Expression*
+script_exp_function_length(void* closurev, const char* name, size_t namelen)
+{
+ Parser_closure* closure = static_cast<Parser_closure*>(closurev);
+ Script_sections* ss = closure->script_options()->script_sections();
+ Expression* length = ss->find_memory_region_length(name, namelen);
+
+ if (length == NULL)
+ {
+ gold_error(_("undefined memory region '%s' referenced "
+ "in LENGTH expression"),
+ name);
+ // Create a dummy expression to prevent crashes later on.
+ length = script_exp_integer(0);
+ }
+
+ return length;