This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
[patch] Refactor duplicated code into a helper function
- From: Rafael EspÃndola <rafael dot espindola at gmail dot com>
- To: Binutils <binutils at sourceware dot org>
- Cc: Cary Coutant <ccoutant at google dot com>
- Date: Wed, 4 Feb 2015 11:11:26 -0500
- Subject: [patch] Refactor duplicated code into a helper function
- Authentication-results: sourceware.org; auth=none
The attached patch refactors a bit of duplicated code into a helper
function. It will make it easier to handle both cases when gcing parts
of mergeable sections.
This includes a mini bug fix in that an unaligned string that is also
not null terminated is reported as unaligned.
2014-09-18 Rafael Ãvila de EspÃndola <rafael.espindola@gmail.com>
* merge.h (add_string): declare.
* merge.cc (add_string): define.
(do_add_input_section): use add_string.
Cheers,
Rafael
diff --git a/gold/merge.cc b/gold/merge.cc
index 269e6bf..c2ee5cc 100644
--- a/gold/merge.cc
+++ b/gold/merge.cc
@@ -497,6 +497,26 @@ Output_merge_data::do_print_merge_stats(const char* section_name)
}
// Class Output_merge_string.
+template <typename Char_type>
+void
+Output_merge_string<Char_type>::add_string(const Char_type *&p, size_t len,
+ uintptr_t init_align_modulo,
+ bool &has_misaligned_strings,
+ Merged_strings &merged_strings,
+ section_size_type &i) {
+ // Within merge input section each string must be aligned.
+ if (len != 0
+ && ((reinterpret_cast<uintptr_t>(p) & (this->addralign() - 1))
+ != init_align_modulo))
+ has_misaligned_strings = true;
+
+ Stringpool::Key key;
+ this->stringpool_.add_with_length(p, len, true, &key);
+
+ merged_strings.push_back(Merged_string(i, key));
+ i += (len + 1) * sizeof(Char_type);
+ p += len + 1;
+}
// Add an input section to a merged string section.
@@ -567,30 +587,14 @@ Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
while (p < pend0)
{
size_t len = string_length(p);
-
- // Within merge input section each string must be aligned.
- if (len != 0
- && ((reinterpret_cast<uintptr_t>(p) & (this->addralign() - 1))
- != init_align_modulo))
- has_misaligned_strings = true;
-
- Stringpool::Key key;
- this->stringpool_.add_with_length(p, len, true, &key);
-
- merged_strings.push_back(Merged_string(i, key));
- p += len + 1;
- i += (len + 1) * sizeof(Char_type);
+ add_string(p, len, init_align_modulo, has_misaligned_strings,
+ merged_strings, i);
}
if (p < pend)
{
size_t len = pend - p;
-
- Stringpool::Key key;
- this->stringpool_.add_with_length(p, len, true, &key);
-
- merged_strings.push_back(Merged_string(i, key));
-
- i += (len + 1) * sizeof(Char_type);
+ add_string(p, len, init_align_modulo, has_misaligned_strings,
+ merged_strings, i);
}
// Record the last offset in the input section so that we can
diff --git a/gold/merge.h b/gold/merge.h
index 6318f45..d233921 100644
--- a/gold/merge.h
+++ b/gold/merge.h
@@ -542,6 +542,11 @@ class Output_merge_string : public Output_merge_base
typedef std::vector<Merged_string> Merged_strings;
+ void
+ add_string(const Char_type *&p, size_t len, uintptr_t init_align_modulo,
+ bool &has_misaligned_strings, Merged_strings &merged_strings,
+ section_size_type &i);
+
struct Merged_strings_list
{
// The input object where the strings were found.