This is an archive of the discontinued LLVM Phabricator instance.

[BinaryFormat] Add DT_USED tag into dynamic section.
ClosedPublic

Authored by Higuoxing on Feb 27 2019, 11:00 PM.

Details

Summary

This tag is documented in https://docs.oracle.com/cd/E19253-01/817-1984/chapter6-42444/index.html
Though I could not find some docs that describe it in detail, I found some code snippets.

	/*
	 * Look up the string in the string table and get its offset. If
	 * this succeeds, then it is possible that there is a DT_NEEDED
	 * dynamic entry that references it.
	 */
	have_string = elfedit_sec_findstr(argstate->str.sec,
	    strpad_elt.dn_dyn.d_un.d_val, arg, &str_offset) != 0;
	if (have_string) {
		dyn = argstate->dynamic.data;
		for (ndx = 0; ndx < numdyn; dyn++, ndx++) {
			if (((dyn->d_tag == DT_NEEDED) ||
			    (dyn->d_tag == DT_USED)) &&
			    (dyn->d_un.d_val == str_offset))
				goto done;
		}
	}

https://github.com/kofemann/opensolaris/blob/80192cd83bf665e708269dae856f9145f7190f74/usr/src/cmd/sgs/elfedit/modules/common/syminfo.c#L512

case DT_USED:
case DT_INIT_ARRAY:
case DT_FINI_ARRAY:
  if (do_dynamic)
    {
      if (entry->d_tag == DT_USED
      && VALID_DYNAMIC_NAME (entry->d_un.d_val))
    {
      char *name = GET_DYNAMIC_NAME (entry->d_un.d_val);

      if (*name)
        {
          printf (_("Not needed object: [%s]\n"), name);
          break;
        }
    }

      print_vma (entry->d_un.d_val, PREFIX_HEX);
      putchar ('\n');
    }
  break;

http://web.mit.edu/freebsd/head/contrib/binutils/binutils/readelf.c

#define DT_USED     0x7ffffffe  /* ignored - same as needed */

https://github.com/switchbrew/switch-tools/blob/master/src/elf_common.h

Diff Detail

Event Timeline

Higuoxing created this revision.Feb 27 2019, 11:00 PM
Higuoxing retitled this revision from [BinaryFormat] Add DT_USED tag into dynamic section. <<Replace this line with your revision title> to [BinaryFormat] Add DT_USED tag into dynamic section..Feb 27 2019, 11:00 PM

I think it is OK, but needs a test.
You could probably update the test\tools\obj2yaml\dynamic-section.test to test this bit.

Address @grimar 's comment.

  • Add one test
grimar added a comment.EditedFeb 28 2019, 12:39 AM

LGTM, but please wait for @jhenderson approvement just in case.
(at first, I was wondering if it should be placed to dynamic-section-arch-tags.test instead since these flags seem
to be sun specific extensions. I think placing in dynamic-section.test is fine, because in DynamicTags.def we handle them as common tags).

Side note: we do not test DT_AUXILIARY and DT_FILTER. We might want to fix it for yaml2obj/ob2yaml too.

grimar accepted this revision.Feb 28 2019, 12:40 AM
This revision is now accepted and ready to land.Feb 28 2019, 12:40 AM

Hmmm... I'm not sure about this. The code and test look fine, but I'm wondering if we should even support this? (I have similar concerns re. DT_AUXILIARY and DT_FILTER by the way, but they're already recognised by LLVM). In particular, this is in the processor-specific tag range, so could conflict with the value some developers have used for their own processor-specific tag. Some questions before I give this the thumbs up:

  1. Do GNU objdump and readelf recognise this flag?
  2. Do you know of any actual users of this tag?
  3. Is there documentation for these tags anywhere other than the Oracle/Sun documentation? I'm not talking about code examples here by the way.

FWIW DT_AUXILIARY and DT_FILTER are supported by LLD.

FWIW DT_AUXILIARY and DT_FILTER are supported by LLD.

Yes, hence why I'm less fussed about those. We already support them in other parts of LLVM, but we don't DT_USED, right?

FWIW DT_AUXILIARY and DT_FILTER are supported by LLD.

Yes, hence why I'm less fussed about those. We already support them in other parts of LLVM, but we don't DT_USED, right?

AFAIK - yes. It might be a good idea to support in in readelf though. If somebody one day will want to use its value for something, he/she
be able to find out that the flag is already used. At least in the code in the wild. I think it's better to try not to reuse the existent values.

Sorry for the delay.

  1. Do GNU objdump and readelf recognise this flag?

Yes, both GNU objdump and GNU readelf recognize this flag. GNU objdump prints its value in hex format, while GNU readelf prints its value in string that the value points to.

  1. Do you know of any actual users of this tag?

No.

  1. Is there documentation for these tags anywhere other than the Oracle/Sun documentation? I'm not talking about code examples here by the way.

I've tried to find some docs that could give us information on it in detail, but I cannot find one that makes me happy.

What I found is:

NameValued_unExecutableShared Object
DT_USED0x7ffffffed_valOptionalOptional

from: Dynamic Section

All my knowledge about this tag is from some codes segment.

jhenderson accepted this revision.Mar 5 2019, 5:37 AM

Okay, LGTM. @grimar's logic is convincing enough to me, when combined with GNU's support for it.

This revision was automatically updated to reflect the committed changes.