Add icf_virtual_function_folding_test to check_PROGRAMS.
[binutils-gdb.git] / gold / expression.cc
index 25a19f9ae495922d4c70eb32e27ad2e7d51c31b5..853a698d187b1fc99a5a28ba5a49ba43eeafac8f 100644 (file)
@@ -54,6 +54,8 @@ struct Expression::Expression_eval_info
   const Symbol_table* symtab;
   // The layout--we use this to get section information.
   const Layout* layout;
+  // Whether to check assertions.
+  bool check_assertions;
   // Whether expressions can refer to the dot symbol.  The dot symbol
   // is only available within a SECTIONS clause.
   bool is_dot_available;
@@ -69,21 +71,24 @@ struct Expression::Expression_eval_info
 // Evaluate an expression.
 
 uint64_t
-Expression::eval(const Symbol_table* symtab, const Layout* layout)
+Expression::eval(const Symbol_table* symtab, const Layout* layout,
+                bool check_assertions)
 {
   Output_section* dummy;
-  return this->eval_maybe_dot(symtab, layout, false, 0, NULL, &dummy);
+  return this->eval_maybe_dot(symtab, layout, check_assertions,
+                             false, 0, NULL, &dummy);
 }
 
 // Evaluate an expression which may refer to the dot symbol.
 
 uint64_t
 Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
-                         uint64_t dot_value, Output_section* dot_section,
+                         bool check_assertions, uint64_t dot_value,
+                         Output_section* dot_section,
                          Output_section** result_section_pointer)
 {
-  return this->eval_maybe_dot(symtab, layout, true, dot_value, dot_section,
-                             result_section_pointer);
+  return this->eval_maybe_dot(symtab, layout, check_assertions, true,
+                             dot_value, dot_section, result_section_pointer);
 }
 
 // Evaluate an expression which may or may not refer to the dot
@@ -91,13 +96,14 @@ Expression::eval_with_dot(const Symbol_table* symtab, const Layout* layout,
 
 uint64_t
 Expression::eval_maybe_dot(const Symbol_table* symtab, const Layout* layout,
-                          bool is_dot_available, uint64_t dot_value,
-                          Output_section* dot_section,
+                          bool check_assertions, bool is_dot_available,
+                          uint64_t dot_value, Output_section* dot_section,
                           Output_section** result_section_pointer)
 {
   Expression_eval_info eei;
   eei.symtab = symtab;
   eei.layout = layout;
+  eei.check_assertions = check_assertions;
   eei.is_dot_available = is_dot_available;
   eei.dot_value = dot_value;
   eei.dot_section = dot_section;
@@ -237,6 +243,7 @@ class Unary_expression : public Expression
            Output_section** arg_section_pointer) const
   {
     return this->arg_->eval_maybe_dot(eei->symtab, eei->layout,
+                                     eei->check_assertions,
                                      eei->is_dot_available,
                                      eei->dot_value,
                                      eei->dot_section,
@@ -313,6 +320,7 @@ class Binary_expression : public Expression
             Output_section** section_pointer) const
   {
     return this->left_->eval_maybe_dot(eei->symtab, eei->layout,
+                                      eei->check_assertions,
                                       eei->is_dot_available,
                                       eei->dot_value,
                                       eei->dot_section,
@@ -324,6 +332,7 @@ class Binary_expression : public Expression
              Output_section** section_pointer) const
   {
     return this->right_->eval_maybe_dot(eei->symtab, eei->layout,
+                                       eei->check_assertions,
                                        eei->is_dot_available,
                                        eei->dot_value,
                                        eei->dot_section,
@@ -456,6 +465,7 @@ class Trinary_expression : public Expression
             Output_section** section_pointer) const
   {
     return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
+                                      eei->check_assertions,
                                       eei->is_dot_available,
                                       eei->dot_value,
                                       eei->dot_section,
@@ -467,6 +477,7 @@ class Trinary_expression : public Expression
             Output_section** section_pointer) const
   {
     return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
+                                      eei->check_assertions,
                                       eei->is_dot_available,
                                       eei->dot_value,
                                       eei->dot_section,
@@ -478,6 +489,7 @@ class Trinary_expression : public Expression
             Output_section** section_pointer) const
   {
     return this->arg1_->eval_maybe_dot(eei->symtab, eei->layout,
+                                      eei->check_assertions,
                                       eei->is_dot_available,
                                       eei->dot_value,
                                       eei->dot_section,
@@ -633,6 +645,11 @@ class Section_expression : public Expression
   value_from_output_section(const Expression_eval_info*,
                            Output_section*) = 0;
 
+  // The child class must implement this.
+  virtual uint64_t
+  value_from_script_output_section(uint64_t address, uint64_t load_address,
+                                   uint64_t addralign, uint64_t size) = 0;
+
   // The child class must implement this.
   virtual const char*
   function_name() const = 0;
@@ -646,14 +663,28 @@ Section_expression::value(const Expression_eval_info* eei)
 {
   const char* section_name = this->section_name_.c_str();
   Output_section* os = eei->layout->find_output_section(section_name);
-  if (os == NULL)
+  if (os != NULL)
+    return this->value_from_output_section(eei, os);
+
+  uint64_t address;
+  uint64_t load_address;
+  uint64_t addralign;
+  uint64_t size;
+  const Script_options* ss = eei->layout->script_options();
+  if (ss->saw_sections_clause())
     {
-      gold_error("%s called on nonexistent output section '%s'",
-                this->function_name(), section_name);
-      return 0;
+      if (ss->script_sections()->get_output_section_info(section_name,
+                                                         &address,
+                                                         &load_address,
+                                                         &addralign,
+                                                         &size))
+        return this->value_from_script_output_section(address, load_address,
+                                                      addralign, size);
     }
 
-  return this->value_from_output_section(eei, os);
+  gold_error("%s called on nonexistent output section '%s'",
+             this->function_name(), section_name);
+  return 0;
 }
 
 // ABSOLUTE function.
@@ -738,7 +769,7 @@ class Assert_expression : public Unary_expression
   value(const Expression_eval_info* eei)
   {
     uint64_t value = this->arg_value(eei, eei->result_section_pointer);
-    if (!value)
+    if (!value && eei->check_assertions)
       gold_error("%s", this->message_.c_str());
     return value;
   }
@@ -780,6 +811,11 @@ class Addr_expression : public Section_expression
     return os->address();
   }
 
+  uint64_t
+  value_from_script_output_section(uint64_t address, uint64_t, uint64_t,
+                                   uint64_t)
+  { return address; }
+
   const char*
   function_name() const
   { return "ADDR"; }
@@ -806,6 +842,11 @@ class Alignof_expression : public Section_expression
                            Output_section* os)
   { return os->addralign(); }
 
+  uint64_t
+  value_from_script_output_section(uint64_t, uint64_t, uint64_t addralign,
+                                   uint64_t)
+  { return addralign; }
+
   const char*
   function_name() const
   { return "ALIGNOF"; }
@@ -976,6 +1017,11 @@ class Loadaddr_expression : public Section_expression
       }
   }
 
+  uint64_t
+  value_from_script_output_section(uint64_t, uint64_t load_address, uint64_t,
+                                   uint64_t)
+  { return load_address; }
+
   const char*
   function_name() const
   { return "LOADADDR"; }
@@ -1008,6 +1054,11 @@ class Sizeof_expression : public Section_expression
     return os->current_data_size();
   }
 
+  uint64_t
+  value_from_script_output_section(uint64_t, uint64_t, uint64_t,
+                                   uint64_t size)
+  { return size; }
+
   const char*
   function_name() const
   { return "SIZEOF"; }
@@ -1062,17 +1113,62 @@ script_exp_function_sizeof_headers()
   return new Sizeof_headers_expression();
 }
 
-// In the GNU linker SEGMENT_START basically returns the value for
-// -Ttext, -Tdata, or -Tbss.  We could implement this by copying the
-// values from General_options to Parameters.  But I doubt that
-// anybody actually uses it.  The point of it for the GNU linker was
-// because -Ttext set the address of the .text section rather than the
-// text segment.  In gold -Ttext sets the text segment address anyhow.
+// SEGMENT_START.
+
+class Segment_start_expression : public Unary_expression
+{
+ public:
+  Segment_start_expression(const char* segment_name, size_t segment_name_len,
+                          Expression* default_value)
+    : Unary_expression(default_value),
+      segment_name_(segment_name, segment_name_len)
+  { }
+
+  uint64_t
+  value(const Expression_eval_info*);
+
+  void
+  print(FILE* f) const
+  {
+    fprintf(f, "SEGMENT_START(\"%s\", ", this->segment_name_.c_str());
+    this->arg_print(f);
+    fprintf(f, ")");
+  }
+
+ private:
+  std::string segment_name_;
+};
+
+uint64_t
+Segment_start_expression::value(const Expression_eval_info* eei)
+{
+  // Check for command line overrides.
+  if (parameters->options().user_set_Ttext()
+      && this->segment_name_ == ".text")
+    return parameters->options().Ttext();
+  else if (parameters->options().user_set_Tdata()
+          && this->segment_name_ == ".data")
+    return parameters->options().Tdata();
+  else if (parameters->options().user_set_Tbss()
+          && this->segment_name_ == ".bss")
+    return parameters->options().Tbss();
+  else
+    {
+      Output_section* dummy;
+      uint64_t ret = this->arg_value(eei, &dummy);
+      // Force the value to be absolute.
+      *eei->result_section_pointer = NULL;
+      return ret;
+    }
+}
 
 extern "C" Expression*
-script_exp_function_segment_start(const char*, size_t, Expression*)
+script_exp_function_segment_start(const char* segment_name,
+                                 size_t segment_name_len,
+                                 Expression* default_value)
 {
-  gold_fatal(_("SEGMENT_START not implemented"));
+  return new Segment_start_expression(segment_name, segment_name_len,
+                                     default_value);
 }
 
 // Functions for memory regions.  These can not be implemented unless