2 * Copyright (c) 2013 Baptiste Daroussin <bapt@FreeBSD.org>
3 * Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/elf_common.h>
34 #include <sys/endian.h>
49 #include "elf_tables.h"
52 #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
62 static struct config_entry c[] = {
66 URL_SCHEME_PREFIX "http://pkg.FreeBSD.org/${ABI}/latest",
84 [ASSUME_ALWAYS_YES] = {
108 elf_corres_to_string(struct _elf_corres *m, int e)
112 for (i = 0; m[i].string != NULL; i++)
113 if (m[i].elf_nb == e)
114 return (m[i].string);
120 aeabi_parse_arm_attributes(void *data, size_t length)
123 uint8_t *section = data;
125 #define MOVE(len) do { \
126 assert(length >= (len)); \
131 if (length == 0 || *section != 'A')
136 /* Read the section length */
137 if (length < sizeof(sect_len))
140 memcpy(§_len, section, sizeof(sect_len));
143 * The section length should be no longer than the section it is within
145 if (sect_len > length)
148 MOVE(sizeof(sect_len));
150 /* Skip the vendor name */
151 while (length != 0) {
152 if (*section == '\0')
160 while (length != 0) {
164 case 1: /* Tag_File */
166 if (length < sizeof(tag_length))
168 memcpy(&tag_length, section, sizeof(tag_length));
170 case 2: /* Tag_Section */
171 case 3: /* Tag_Symbol */
175 /* At least space for the tag and size */
179 /* Check the tag fits */
180 if (tag_length > length)
183 #define MOVE_TAG(len) do { \
184 assert(tag_length >= (len)); \
186 tag_length -= (len); \
189 MOVE(sizeof(tag_length));
190 tag_length -= sizeof(tag_length);
192 while (tag_length != 0) {
195 assert(tag_length >= length);
201 * These tag values come from:
203 * Addenda to, and Errata in, the ABI for the
204 * ARM Architecture. Release 2.08, section 2.3.
206 if (tag == 6) { /* == Tag_CPU_arch */
211 * We don't support values that require
212 * more than one byte.
217 /* We have an ARMv4 or ARMv5 */
220 else /* We have an ARMv6+ */
222 } else if (tag == 4 || tag == 5 || tag == 32 ||
223 tag == 65 || tag == 67) {
224 while (*section != '\0' && length != 0)
228 /* Skip the last byte */
230 } else if ((tag >= 7 && tag <= 31) || tag == 34 ||
231 tag == 36 || tag == 38 || tag == 42 || tag == 44 ||
232 tag == 64 || tag == 66 || tag == 68 || tag == 70) {
233 /* Skip the uleb128 data */
234 while (*section & (1 << 7) && length != 0)
238 /* Skip the last byte */
252 pkg_get_myabi(char *dest, size_t sz)
259 const char *arch, *abi, *fpu, *endian_corres_str;
260 const char *wordsize_corres_str;
271 if (elf_version(EV_CURRENT) == EV_NONE) {
272 warnx("ELF library initialization failed: %s",
277 if ((fd = open(_PATH_BSHELL, O_RDONLY)) < 0) {
282 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
284 warnx("elf_begin() failed: %s.", elf_errmsg(-1));
288 if (gelf_getehdr(elf, &elfhdr) == NULL) {
290 warn("getehdr() failed: %s.", elf_errmsg(-1));
293 while ((scn = elf_nextscn(elf, scn)) != NULL) {
294 if (gelf_getshdr(scn, &shdr) != &shdr) {
296 warn("getshdr() failed: %s.", elf_errmsg(-1));
300 if (shdr.sh_type == SHT_NOTE)
306 warn("failed to get the note section");
310 data = elf_getdata(scn, NULL);
313 memcpy(¬e, src, sizeof(Elf_Note));
314 src += sizeof(Elf_Note);
315 if (note.n_type == NT_VERSION)
317 src += note.n_namesz + note.n_descsz;
320 src += roundup2(note.n_namesz, 4);
321 if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB)
322 version = be32dec(src);
324 version = le32dec(src);
326 for (i = 0; osname[i] != '\0'; i++)
327 osname[i] = (char)tolower(osname[i]);
329 wordsize_corres_str = elf_corres_to_string(wordsize_corres,
330 (int)elfhdr.e_ident[EI_CLASS]);
332 arch = elf_corres_to_string(mach_corres, (int) elfhdr.e_machine);
334 snprintf(dest, sz, "%s:%d",
335 osname, version / 100000);
339 switch (elfhdr.e_machine) {
341 endian_corres_str = elf_corres_to_string(endian_corres,
342 (int)elfhdr.e_ident[EI_DATA]);
344 /* FreeBSD doesn't support the hard-float ABI yet */
346 if ((elfhdr.e_flags & 0xFF000000) != 0) {
347 const char *sh_name = NULL;
350 /* This is an EABI file, the conformance level is set */
352 /* Find which TARGET_ARCH we are building for. */
353 elf_getshdrstrndx(elf, &shstrndx);
354 while ((scn = elf_nextscn(elf, scn)) != NULL) {
356 if (gelf_getshdr(scn, &shdr) != &shdr) {
361 sh_name = elf_strptr(elf, shstrndx,
365 if (strcmp(".ARM.attributes", sh_name) == 0)
368 if (scn != NULL && sh_name != NULL) {
369 data = elf_getdata(scn, NULL);
371 * Prior to FreeBSD 10.0 libelf would return
372 * NULL from elf_getdata on the .ARM.attributes
373 * section. As this was the first release to
374 * get armv6 support assume a NULL value means
377 * This assumption can be removed when 9.x
381 arch = aeabi_parse_arm_attributes(
382 data->d_buf, data->d_size);
385 warn("unknown ARM ARCH");
391 warn("Unable to find the .ARM.attributes "
395 } else if (elfhdr.e_ident[EI_OSABI] != ELFOSABI_NONE) {
397 * EABI executables all have this field set to
398 * ELFOSABI_NONE, therefore it must be an oabi file.
403 warn("unknown ARM ABI");
406 snprintf(dest + strlen(dest), sz - strlen(dest),
407 ":%s:%s:%s:%s:%s", arch, wordsize_corres_str,
408 endian_corres_str, abi, fpu);
412 * this is taken from binutils sources:
414 * mapping is figured out from binutils:
415 * gas/config/tc-mips.c
417 switch (elfhdr.e_flags & EF_MIPS_ABI) {
425 if (elfhdr.e_ident[EI_DATA] ==
428 else if (elfhdr.e_ident[EI_DATA] ==
433 endian_corres_str = elf_corres_to_string(endian_corres,
434 (int)elfhdr.e_ident[EI_DATA]);
436 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s:%s",
437 arch, wordsize_corres_str, endian_corres_str, abi);
440 snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s",
441 arch, wordsize_corres_str);
453 subst_packagesite(const char *abi)
456 const char *variable_string;
459 if (c[PACKAGESITE].value != NULL)
460 oldval = c[PACKAGESITE].value;
462 oldval = c[PACKAGESITE].val;
464 if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
467 newval = sbuf_new_auto();
468 sbuf_bcat(newval, oldval, variable_string - oldval);
469 sbuf_cat(newval, abi);
470 sbuf_cat(newval, variable_string + strlen("${ABI}"));
473 free(c[PACKAGESITE].value);
474 c[PACKAGESITE].value = strdup(sbuf_data(newval));
478 config_parse(yaml_document_t *doc, yaml_node_t *node, pkg_conf_file_t conftype)
480 yaml_node_pair_t *pair;
481 yaml_node_t *key, *val;
482 struct sbuf *buf = sbuf_new_auto();
486 pair = node->data.mapping.pairs.start;
488 while (pair < node->data.mapping.pairs.top) {
489 key = yaml_document_get_node(doc, pair->key);
490 val = yaml_document_get_node(doc, pair->value);
493 * ignoring silently empty keys can be empty lines
496 if (key->data.scalar.length <= 0) {
502 * silently skip on purpose to allow user to leave
503 * empty lines without complaining
505 if (val->type == YAML_NO_NODE ||
506 (val->type == YAML_SCALAR_NODE &&
507 val->data.scalar.length <= 0)) {
514 if (conftype == CONFFILE_PKG) {
515 for (j = 0; j < strlen(key->data.scalar.value); ++j)
517 toupper(key->data.scalar.value[j]));
519 } else if (conftype == CONFFILE_REPO) {
520 /* The CONFFILE_REPO type is more restrictive. Only
521 parse known elements. */
522 if (strcasecmp(key->data.scalar.value, "url") == 0)
523 sbuf_cpy(buf, "PACKAGESITE");
524 else if (strcasecmp(key->data.scalar.value,
526 sbuf_cpy(buf, "MIRROR_TYPE");
527 else if (strcasecmp(key->data.scalar.value,
528 "signature_type") == 0)
529 sbuf_cpy(buf, "SIGNATURE_TYPE");
530 else if (strcasecmp(key->data.scalar.value,
531 "fingerprints") == 0)
532 sbuf_cpy(buf, "FINGERPRINTS");
533 else { /* Skip unknown entries for future use. */
540 for (i = 0; i < CONFIG_SIZE; i++) {
541 if (strcmp(sbuf_data(buf), c[i].key) == 0)
545 /* Silently skip unknown keys to be future compatible. */
546 if (i == CONFIG_SIZE) {
551 /* env has priority over config file */
557 c[i].value = strdup(val->data.scalar.value);
565 * Parse new repo style configs in style:
572 parse_repo_file(yaml_document_t *doc, yaml_node_t *node)
574 yaml_node_pair_t *pair;
576 pair = node->data.mapping.pairs.start;
577 while (pair < node->data.mapping.pairs.top) {
578 yaml_node_t *key = yaml_document_get_node(doc, pair->key);
579 yaml_node_t *val = yaml_document_get_node(doc, pair->value);
581 if (key->data.scalar.length <= 0) {
586 if (val->type != YAML_MAPPING_NODE) {
591 config_parse(doc, val, CONFFILE_REPO);
598 read_conf_file(const char *confpath, pkg_conf_file_t conftype)
601 yaml_parser_t parser;
605 if ((fp = fopen(confpath, "r")) == NULL) {
607 err(EXIT_FAILURE, "Unable to open configuration "
608 "file %s", confpath);
609 /* no configuration present */
613 yaml_parser_initialize(&parser);
614 yaml_parser_set_input_file(&parser, fp);
615 yaml_parser_load(&parser, &doc);
617 node = yaml_document_get_root_node(&doc);
619 if (node == NULL || node->type != YAML_MAPPING_NODE)
620 warnx("Invalid configuration format, ignoring the "
621 "configuration file %s", confpath);
623 if (conftype == CONFFILE_PKG)
624 config_parse(&doc, node, conftype);
625 else if (conftype == CONFFILE_REPO)
626 parse_repo_file(&doc, node);
629 yaml_document_delete(&doc);
630 yaml_parser_delete(&parser);
640 const char *localbase;
641 char confpath[MAXPATHLEN];
644 for (i = 0; i < CONFIG_SIZE; i++) {
645 val = getenv(c[i].key);
652 localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
653 snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
656 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
660 snprintf(confpath, sizeof(confpath), "/etc/pkg/FreeBSD.conf");
661 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
666 if (c[ABI].val == NULL && c[ABI].value == NULL) {
667 if (pkg_get_myabi(abi, BUFSIZ) != 0)
668 errx(EXIT_FAILURE, "Failed to determine the system "
673 subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
679 config_string(pkg_config_key k, const char **val)
681 if (c[k].type != PKG_CONFIG_STRING)
684 if (c[k].value != NULL)
693 config_bool(pkg_config_key k, bool *val)
697 if (c[k].type != PKG_CONFIG_BOOL)
702 if (c[k].value != NULL)
707 if (strcasecmp(value, "true") == 0 ||
708 strcasecmp(value, "yes") == 0 ||
709 strcasecmp(value, "on") == 0 ||
717 config_finish(void) {
720 for (i = 0; i < CONFIG_SIZE; i++)