Re: ksh bad PS1 ascii octal to char conversion

2013-03-02 Thread Andres Perera
On Sat, Mar 02, 2013 at 03:08:44PM -0800, Philip Guenther wrote:
> On Sat, 2 Mar 2013, Andres Perera wrote:
> > On Sat, Mar 2, 2013 at 5:13 PM, Philip Guenther  
> > wrote:
> ...
> > > Hmm, this is actually changing two things: it's both fixing the problem
> > > that the code is not subtracting '0' from each character before doing the
> > > octal place conversion and it's also changing it to support octal
> > > sequences of fewer than 3 digits.  The latter seems unnecessary and isn't
> > > documented, do we really need/want to add it?
> > 
> > the behaviour is consistent with bash, which is the shell ksh took it from
> > 
> > in addition to that, it's consistent with c string literals and printf(1)
> > 
> > ksh having an doing things differently is a bigger bug than
> > precedence... ps1 isn't exactly crucial in scripts
> 
> Yet it wasn't important enough to mention or document.  Huh.  I'm not 
> interested without a manpage update to match.

new patch below

like before

* fix the bug with PS1 octal conversion

* add 1 .. 3 octal number feature as seen in bash, printf(1), etc.

aditionally

* document the new behaviour in ksh(1)

* change `c' to int; match other routines

Index: lex.c
===
RCS file: /cvs/src/bin/ksh/lex.c,v
retrieving revision 1.46
diff -p -u -r1.46 lex.c
--- lex.c   20 Jan 2013 14:47:46 -  1.46
+++ lex.c   2 Mar 2013 23:07:19 -
@@ -1349,17 +1349,17 @@ dopprompt(const char *sp, int ntruncate,
case '4':
case '5':
case '6':
-   case '7':
-   if ((cp[1] > '7' || cp[1] < '0') ||
-   (cp[2] > '7' || cp[2] < '0')) {
-   snprintf(strbuf, sizeof strbuf,
-   "\\%c", *cp);
-   break;
-   }
-   n = cp[0] * 8 * 8 + cp[1] * 8 + cp[2];
-   snprintf(strbuf, sizeof strbuf, "%c", n);
-   cp += 2;
+   case '7': {
+   int c = 0, i = 3;
+
+   do
+   c = c * 8 + *cp - '0';
+   while (--i && *++cp >= '0' && *cp <= '7');
+   cp -= !!i;
+   strbuf[0] = c;
+   strbuf[1] = '\0';
break;
+   }
case '\\':  /* '\' '\' */
strbuf[0] = '\\';
strbuf[1] = '\0';
Index: ksh.1
===
RCS file: /cvs/src/bin/ksh/ksh.1,v
retrieving revision 1.145
diff -p -u -r1.145 ksh.1
--- ksh.1   17 Jan 2013 21:20:25 -  1.145
+++ ksh.1   2 Mar 2013 23:07:21 -
@@ -1662,7 +1662,7 @@ as a special character within double quo
 it is safer in this case to escape the backslash
 than to try quoting it.
 .It Li \e Ns Ar nnn
-The octal character
+The character whose octal value is the 1 to 3 (inclusive) digit number
 .Ar nnn .
 .It Li \e\e
 Insert a single backslash character.



Re: Small binutils tweak

2013-03-02 Thread Brian Callahan

On 3/2/2013 10:36 AM, Brian Callahan wrote:

Hi tech --

While doing some ports testing with clang, I came across the binutils
bug mentioned here:
http://lists.gnu.org/archive/html/bug-binutils/2004-07/msg0.html

Below is a backport of the commit mentioned later in the thread. It
fixes the issue.
I was able to rebuild working kernels and do a full 'make build' on
amd64, loongson, and macppc with this patch. But since it affects all
archs, testing on the archs I don't have access to will be needed.

OK?



To clear up any confusion, this code is taken from the 2.16 time frame. 
It is GPLv2 code. The fix in question is already in the binutils-2.17 
code we have in our tree.



~Brian

Index: elf-bfd.h
===
RCS file: /cvs/src/gnu/usr.bin/binutils/bfd/elf-bfd.h,v
retrieving revision 1.8
diff -u -p -r1.8 elf-bfd.h
--- elf-bfd.h2 Nov 2004 20:45:06 -1.8
+++ elf-bfd.h16 Feb 2013 22:43:53 -
@@ -1055,8 +1055,8 @@ struct bfd_elf_section_data
  #define elf_discarded_section(sec)\
(!bfd_is_abs_section (sec)\
 && bfd_is_abs_section ((sec)->output_section) \
-   && sec->sec_info_type != ELF_INFO_TYPE_MERGE \
-   && sec->sec_info_type != ELF_INFO_TYPE_JUST_SYMS)
+   && (sec)->sec_info_type != ELF_INFO_TYPE_MERGE  \
+   && (sec)->sec_info_type != ELF_INFO_TYPE_JUST_SYMS)

  #define get_elf_backend_data(abfd) \
((const struct elf_backend_data *) (abfd)->xvec->backend_data)
Index: elflink.c
===
RCS file: /cvs/src/gnu/usr.bin/binutils/bfd/elflink.c,v
retrieving revision 1.9
diff -u -p -r1.9 elflink.c
--- elflink.c24 Nov 2004 16:55:31 -1.9
+++ elflink.c16 Feb 2013 22:43:56 -
@@ -6239,6 +6239,9 @@ elf_link_output_extsym (struct elf_link_
return TRUE;
  }

+/* Return TRUE if special handling is done for relocs in SEC against
+   symbols defined in discarded sections.  */
+
  static bfd_boolean
  elf_section_ignore_discarded_relocs (asection *sec)
  {
@@ -6261,6 +6264,26 @@ elf_section_ignore_discarded_relocs (ase
return FALSE;
  }

+/* Return TRUE if we should complain about a reloc in SEC against a
+   symbol defined in a discarded section.  */
+
+static bfd_boolean
+elf_section_complain_discarded (asection *sec)
+{
+  if (strncmp (".stab", sec->name, 5) == 0
+  && (!sec->name[5] ||
+ (sec->name[5] == '.' && ISDIGIT (sec->name[6]
+return FALSE;
+
+  if (strcmp (".eh_frame", sec->name) == 0)
+return FALSE;
+
+  if (strcmp (".gcc_except_table", sec->name) == 0)
+return FALSE;
+
+  return TRUE;
+}
+
  /* Link an input file into the linker output file.  This function
 handles all the sections and relocations of the input file at once.
 This is so that we only have to read the local symbols once, and
@@ -6532,13 +6555,16 @@ elf_link_input_bfd (struct elf_final_lin
if (!elf_section_ignore_discarded_relocs (o))
  {
Elf_Internal_Rela *rel, *relend;
+  bfd_boolean complain = elf_section_complain_discarded (o);

rel = internal_relocs;
relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
for ( ; rel < relend; rel++)
  {
unsigned long r_symndx = rel->r_info >> r_sym_shift;
-  asection *sec;
+  asection **ps, *sec;
+  struct elf_link_hash_entry *h = NULL;
+  const char *sym_name;

if (r_symndx >= locsymcount
|| (elf_bad_symtab (input_bfd)
@@ -6551,79 +6577,70 @@ elf_link_input_bfd (struct elf_final_lin
   || h->root.type == bfd_link_hash_warning)
  h = (struct elf_link_hash_entry *) h->root.u.i.link;

-  /* Complain if the definition comes from a
- discarded section.  */
-  sec = h->root.u.def.section;
-  if ((h->root.type == bfd_link_hash_defined
-   || h->root.type == bfd_link_hash_defweak)
-  && elf_discarded_section (sec))
-{
-  if ((o->flags & SEC_DEBUGGING) != 0)
-{
-  BFD_ASSERT (r_symndx != 0);
-  /* Try to preserve debug information.  */
-  if ((o->flags & SEC_DEBUGGING) != 0
-  && sec->kept_section != NULL
-  && sec->_raw_size == sec->kept_section->_raw_size)
-h->root.u.def.section
-  = sec->kept_section;
-  else
-memset (rel, 0, sizeof (*rel));
-}
-  else
-finfo->info->callbacks->error_handler
-  (LD_DEFINITION_IN_DISCARDED_SECTION,
-   _("%T: discarded in section `%s' from %s\n"),
-   h->root.root.string,
-   h->root.root.string,
-   h->root.u.def.section->name,
- 

Small binutils tweak

2013-03-02 Thread Brian Callahan

Hi tech --

While doing some ports testing with clang, I came across the binutils 
bug mentioned here:

http://lists.gnu.org/archive/html/bug-binutils/2004-07/msg0.html

Below is a backport of the commit mentioned later in the thread. It 
fixes the issue.
I was able to rebuild working kernels and do a full 'make build' on 
amd64, loongson, and macppc with this patch. But since it affects all 
archs, testing on the archs I don't have access to will be needed.


OK?

~Brian

Index: elf-bfd.h
===
RCS file: /cvs/src/gnu/usr.bin/binutils/bfd/elf-bfd.h,v
retrieving revision 1.8
diff -u -p -r1.8 elf-bfd.h
--- elf-bfd.h2 Nov 2004 20:45:06 -1.8
+++ elf-bfd.h16 Feb 2013 22:43:53 -
@@ -1055,8 +1055,8 @@ struct bfd_elf_section_data
 #define elf_discarded_section(sec)\
   (!bfd_is_abs_section (sec)\
&& bfd_is_abs_section ((sec)->output_section) \
-   && sec->sec_info_type != ELF_INFO_TYPE_MERGE \
-   && sec->sec_info_type != ELF_INFO_TYPE_JUST_SYMS)
+   && (sec)->sec_info_type != ELF_INFO_TYPE_MERGE  \
+   && (sec)->sec_info_type != ELF_INFO_TYPE_JUST_SYMS)

 #define get_elf_backend_data(abfd) \
   ((const struct elf_backend_data *) (abfd)->xvec->backend_data)
Index: elflink.c
===
RCS file: /cvs/src/gnu/usr.bin/binutils/bfd/elflink.c,v
retrieving revision 1.9
diff -u -p -r1.9 elflink.c
--- elflink.c24 Nov 2004 16:55:31 -1.9
+++ elflink.c16 Feb 2013 22:43:56 -
@@ -6239,6 +6239,9 @@ elf_link_output_extsym (struct elf_link_
   return TRUE;
 }

+/* Return TRUE if special handling is done for relocs in SEC against
+   symbols defined in discarded sections.  */
+
 static bfd_boolean
 elf_section_ignore_discarded_relocs (asection *sec)
 {
@@ -6261,6 +6264,26 @@ elf_section_ignore_discarded_relocs (ase
   return FALSE;
 }

+/* Return TRUE if we should complain about a reloc in SEC against a
+   symbol defined in a discarded section.  */
+
+static bfd_boolean
+elf_section_complain_discarded (asection *sec)
+{
+  if (strncmp (".stab", sec->name, 5) == 0
+  && (!sec->name[5] ||
+ (sec->name[5] == '.' && ISDIGIT (sec->name[6]
+return FALSE;
+
+  if (strcmp (".eh_frame", sec->name) == 0)
+return FALSE;
+
+  if (strcmp (".gcc_except_table", sec->name) == 0)
+return FALSE;
+
+  return TRUE;
+}
+
 /* Link an input file into the linker output file.  This function
handles all the sections and relocations of the input file at once.
This is so that we only have to read the local symbols once, and
@@ -6532,13 +6555,16 @@ elf_link_input_bfd (struct elf_final_lin
   if (!elf_section_ignore_discarded_relocs (o))
 {
   Elf_Internal_Rela *rel, *relend;
+  bfd_boolean complain = elf_section_complain_discarded (o);

   rel = internal_relocs;
   relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
   for ( ; rel < relend; rel++)
 {
   unsigned long r_symndx = rel->r_info >> r_sym_shift;
-  asection *sec;
+  asection **ps, *sec;
+  struct elf_link_hash_entry *h = NULL;
+  const char *sym_name;

   if (r_symndx >= locsymcount
   || (elf_bad_symtab (input_bfd)
@@ -6551,79 +6577,70 @@ elf_link_input_bfd (struct elf_final_lin
  || h->root.type == bfd_link_hash_warning)
 h = (struct elf_link_hash_entry *) h->root.u.i.link;

-  /* Complain if the definition comes from a
- discarded section.  */
-  sec = h->root.u.def.section;
-  if ((h->root.type == bfd_link_hash_defined
-   || h->root.type == bfd_link_hash_defweak)
-  && elf_discarded_section (sec))
-{
-  if ((o->flags & SEC_DEBUGGING) != 0)
-{
-  BFD_ASSERT (r_symndx != 0);
-  /* Try to preserve debug information.  */
-  if ((o->flags & SEC_DEBUGGING) != 0
-  && sec->kept_section != NULL
-  && sec->_raw_size == sec->kept_section->_raw_size)
-h->root.u.def.section
-  = sec->kept_section;
-  else
-memset (rel, 0, sizeof (*rel));
-}
-  else
-finfo->info->callbacks->error_handler
-  (LD_DEFINITION_IN_DISCARDED_SECTION,
-   _("%T: discarded in section `%s' from %s\n"),
-   h->root.root.string,
-   h->root.root.string,
-   h->root.u.def.section->name,
-   bfd_archive_filename (h->root.u.def.section->owner));
-}
-}
-  else
-{
-  sec = finfo->sections[r_symndx];
-
-  if (sec != NULL && elf_discarded_section (sec))
-