2009-08-12 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / gold / expression.cc
index d7536016b0fb3d07338fe4030566bb5b01edbb35..25f3ac33936a30a604d6af5a3b68ebecc733a933 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;
@@ -170,9 +176,9 @@ Symbol_expression::value(const Expression_eval_info* eei)
 
   *eei->result_section_pointer = sym->output_section();
 
-  if (parameters->get_size() == 32)
+  if (parameters->target().get_size() == 32)
     return eei->symtab->get_sized_symbol<32>(sym)->value();
-  else if (parameters->get_size() == 64)
+  else if (parameters->target().get_size() == 64)
     return eei->symtab->get_sized_symbol<64>(sym)->value();
   else
     gold_unreachable();
@@ -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,
@@ -267,7 +274,7 @@ class Unary_expression : public Expression
     {                                                                  \
       Output_section* arg_section;                                     \
       uint64_t ret = OPERATOR this->arg_value(eei, &arg_section);      \
-      if (arg_section != NULL && parameters->output_is_object())       \
+      if (arg_section != NULL && parameters->options().relocatable())  \
        gold_warning(_("unary " #NAME " applied to section "            \
                       "relative value"));                              \
       return ret;                                                      \
@@ -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,
@@ -387,7 +396,7 @@ class Binary_expression : public Expression
        *eei->result_section_pointer = left_section;                    \
       else if ((WARN || left_section != right_section)                 \
               && (left_section != NULL || right_section != NULL)       \
-              && parameters->output_is_object())                       \
+              && parameters->options().relocatable())                  \
        gold_warning(_("binary " #NAME " applied to section "           \
                       "relative value"));                              \
       if (IS_DIV && right == 0)                                                \
@@ -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,
@@ -559,7 +571,7 @@ class Max_expression : public Binary_expression
     if (left_section == right_section)
       *eei->result_section_pointer = left_section;
     else if ((left_section != NULL || right_section != NULL)
-            && parameters->output_is_object())
+            && parameters->options().relocatable())
       gold_warning(_("max applied to section relative value"));
     return std::max(left, right);
   }
@@ -594,7 +606,7 @@ class Min_expression : public Binary_expression
     if (left_section == right_section)
       *eei->result_section_pointer = left_section;
     else if ((left_section != NULL || right_section != NULL)
-            && parameters->output_is_object())
+            && parameters->options().relocatable())
       gold_warning(_("min applied to section relative value"));
     return std::min(left, right);
   }
@@ -610,7 +622,106 @@ script_exp_function_min(Expression* left, Expression* right)
   return new Min_expression(left, right);
 }
 
-// Align function.
+// Class Section_expression.  This is a parent class used for
+// functions which take the name of an output section.
+
+class Section_expression : public Expression
+{
+ public:
+  Section_expression(const char* section_name, size_t section_name_len)
+    : section_name_(section_name, section_name_len)
+  { }
+
+  uint64_t
+  value(const Expression_eval_info*);
+
+  void
+  print(FILE* f) const
+  { fprintf(f, "%s(%s)", this->function_name(), this->section_name_.c_str()); }
+
+ protected:
+  // The child class must implement this.
+  virtual uint64_t
+  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;
+
+ private:
+  std::string section_name_;
+};
+
+uint64_t
+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)
+    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())
+    {
+      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);
+    }
+
+  gold_error("%s called on nonexistent output section '%s'",
+             this->function_name(), section_name);
+  return 0;
+}
+
+// ABSOLUTE function.
+
+class Absolute_expression : public Unary_expression
+{
+ public:
+  Absolute_expression(Expression* arg)
+    : Unary_expression(arg)
+  { }
+
+  uint64_t
+  value(const Expression_eval_info* eei)
+  {
+    Output_section* dummy;
+    uint64_t ret = this->arg_value(eei, &dummy);
+    // Force the value to be absolute.
+    *eei->result_section_pointer = NULL;
+    return ret;
+  }
+
+  void
+  print(FILE* f) const
+  {
+    fprintf(f, "ABSOLUTE(");
+    this->arg_print(f);
+    fprintf(f, ")");
+  }
+};
+
+extern "C" Expression*
+script_exp_function_absolute(Expression* arg)
+{
+  return new Absolute_expression(arg);
+}
+
+// ALIGN function.
 
 class Align_expression : public Binary_expression
 {
@@ -625,7 +736,7 @@ class Align_expression : public Binary_expression
     Output_section* align_section;
     uint64_t align = this->right_value(eei, &align_section);
     if (align_section != NULL
-       && parameters->output_is_object())
+       && parameters->options().relocatable())
       gold_warning(_("aligning to section relative value"));
 
     uint64_t value = this->left_value(eei, eei->result_section_pointer);
@@ -645,7 +756,7 @@ script_exp_function_align(Expression* left, Expression* right)
   return new Align_expression(left, right);
 }
 
-// Assert function.
+// ASSERT function.
 
 class Assert_expression : public Unary_expression
 {
@@ -658,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;
   }
@@ -682,47 +793,69 @@ script_exp_function_assert(Expression* expr, const char* message,
   return new Assert_expression(expr, message, length);
 }
 
-// Addr function.
+// ADDR function.
 
-class Addr_expression : public Expression
+class Addr_expression : public Section_expression
 {
  public:
   Addr_expression(const char* section_name, size_t section_name_len)
-    : section_name_(section_name, section_name_len)
+    : Section_expression(section_name, section_name_len)
   { }
 
+ protected:
   uint64_t
-  value(const Expression_eval_info*);
+  value_from_output_section(const Expression_eval_info* eei,
+                           Output_section* os)
+  {
+    *eei->result_section_pointer = os;
+    return os->address();
+  }
 
-  void
-  print(FILE* f) const
-  { fprintf(f, "ADDR(%s)", this->section_name_.c_str()); }
+  uint64_t
+  value_from_script_output_section(uint64_t address, uint64_t, uint64_t,
+                                   uint64_t)
+  { return address; }
 
- private:
-  std::string section_name_;
+  const char*
+  function_name() const
+  { return "ADDR"; }
 };
 
-uint64_t
-Addr_expression::value(const Expression_eval_info* eei)
+extern "C" Expression*
+script_exp_function_addr(const char* section_name, size_t section_name_len)
 {
-  const char* section_name = this->section_name_.c_str();
-  Output_section* os = eei->layout->find_output_section(section_name);
-  if (os == NULL)
-    {
-      gold_error("ADDR called on nonexistent output section '%s'",
-                section_name);
-      return 0;
-    }
+  return new Addr_expression(section_name, section_name_len);
+}
 
-  *eei->result_section_pointer = os;
+// ALIGNOF.
 
-  return os->address();
-}
+class Alignof_expression : public Section_expression
+{
+ public:
+  Alignof_expression(const char* section_name, size_t section_name_len)
+    : Section_expression(section_name, section_name_len)
+  { }
+
+ protected:
+  uint64_t
+  value_from_output_section(const Expression_eval_info*,
+                           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"; }
+};
 
 extern "C" Expression*
-script_exp_function_addr(const char* section_name, size_t section_name_len)
+script_exp_function_alignof(const char* section_name, size_t section_name_len)
 {
-  return new Addr_expression(section_name, section_name_len);
+  return new Alignof_expression(section_name, section_name_len);
 }
 
 // CONSTANT.  It would be nice if we could simply evaluate this
@@ -770,9 +903,9 @@ Constant_expression::value(const Expression_eval_info*)
   switch (this->function_)
     {
     case CONSTANT_MAXPAGESIZE:
-      return parameters->target()->abi_pagesize();
+      return parameters->target().abi_pagesize();
     case CONSTANT_COMMONPAGESIZE:
-      return parameters->target()->common_pagesize();
+      return parameters->target().common_pagesize();
     default:
       gold_unreachable();
     }
@@ -831,6 +964,112 @@ script_exp_function_data_segment_end(Expression* val)
   return val;
 }
 
+// DEFINED function.
+
+class Defined_expression : public Expression
+{
+ public:
+  Defined_expression(const char* symbol_name, size_t symbol_name_len)
+    : symbol_name_(symbol_name, symbol_name_len)
+  { }
+
+  uint64_t
+  value(const Expression_eval_info* eei)
+  {
+    Symbol* sym = eei->symtab->lookup(this->symbol_name_.c_str());
+    return sym != NULL && sym->is_defined();
+  }
+
+  void
+  print(FILE* f) const
+  { fprintf(f, "DEFINED(%s)", this->symbol_name_.c_str()); }
+
+ private:
+  std::string symbol_name_;
+};
+
+extern "C" Expression*
+script_exp_function_defined(const char* symbol_name, size_t symbol_name_len)
+{
+  return new Defined_expression(symbol_name, symbol_name_len);
+}
+
+// LOADADDR function
+
+class Loadaddr_expression : public Section_expression
+{
+ public:
+  Loadaddr_expression(const char* section_name, size_t section_name_len)
+    : Section_expression(section_name, section_name_len)
+  { }
+
+ protected:
+  uint64_t
+  value_from_output_section(const Expression_eval_info* eei,
+                           Output_section* os)
+  {
+    if (os->has_load_address())
+      return os->load_address();
+    else
+      {
+       *eei->result_section_pointer = os;
+       return os->address();
+      }
+  }
+
+  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"; }
+};
+
+extern "C" Expression*
+script_exp_function_loadaddr(const char* section_name, size_t section_name_len)
+{
+  return new Loadaddr_expression(section_name, section_name_len);
+}
+
+// SIZEOF function
+
+class Sizeof_expression : public Section_expression
+{
+ public:
+  Sizeof_expression(const char* section_name, size_t section_name_len)
+    : Section_expression(section_name, section_name_len)
+  { }
+
+ protected:
+  uint64_t
+  value_from_output_section(const Expression_eval_info*,
+                           Output_section* os)
+  {
+    // We can not use data_size here, as the size of the section may
+    // not have been finalized.  Instead we get whatever the current
+    // size is.  This will work correctly for backward references in
+    // linker scripts.
+    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"; }
+};
+
+extern "C" Expression*
+script_exp_function_sizeof(const char* section_name, size_t section_name_len)
+{
+  return new Sizeof_expression(section_name, section_name_len);
+}
+
 // SIZEOF_HEADERS.
 
 class Sizeof_headers_expression : public Expression
@@ -852,12 +1091,12 @@ Sizeof_headers_expression::value(const Expression_eval_info* eei)
 {
   unsigned int ehdr_size;
   unsigned int phdr_size;
-  if (parameters->get_size() == 32)
+  if (parameters->target().get_size() == 32)
     {
       ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
       phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
     }
-  else if (parameters->get_size() == 64)
+  else if (parameters->target().get_size() == 64)
     {
       ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
       phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
@@ -874,31 +1113,21 @@ script_exp_function_sizeof_headers()
   return new Sizeof_headers_expression();
 }
 
-// Functions.
-
-extern "C" Expression*
-script_exp_function_defined(const char*, size_t)
-{
-  gold_fatal(_("DEFINED not implemented"));
-}
-
-extern "C" Expression*
-script_exp_function_alignof(const char*, size_t)
-{
-  gold_fatal(_("ALIGNOF not implemented"));
-}
+// 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.
 
 extern "C" Expression*
-script_exp_function_sizeof(const char*, size_t)
+script_exp_function_segment_start(const char*, size_t, Expression*)
 {
-  gold_fatal(_("SIZEOF not implemented"));
+  gold_fatal(_("SEGMENT_START not implemented"));
 }
 
-extern "C" Expression*
-script_exp_function_loadaddr(const char*, size_t)
-{
-  gold_fatal(_("LOADADDR not implemented"));
-}
+// Functions for memory regions.  These can not be implemented unless
+// and until we implement memory regions.
 
 extern "C" Expression*
 script_exp_function_origin(const char*, size_t)
@@ -912,16 +1141,4 @@ script_exp_function_length(const char*, size_t)
   gold_fatal(_("LENGTH not implemented"));
 }
 
-extern "C" Expression*
-script_exp_function_absolute(Expression*)
-{
-  gold_fatal(_("ABSOLUTE not implemented"));
-}
-
-extern "C" Expression*
-script_exp_function_segment_start(const char*, size_t, Expression*)
-{
-  gold_fatal(_("SEGMENT_START not implemented"));
-}
-
 } // End namespace gold.
This page took 0.050416 seconds and 4 git commands to generate.